Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Recently I downloaded some badly formated SRT files, having the time format messed up as

HH:MM:SS:XXX

whereas it should be

HH:MM:SS,XXX

The main problem now is to replace the third colon with a comma in every timing line formatted as

HH:MM:SS,XXX --> HH:MM:SS,XX

Can anyone suggest a sed/awk script to fix this problem in the entire file? I did try

sed 's/:/,/3;s/:/,/5' file

but it didn't seem to work

Edit: Answered my own question, having forgotten all about backreferences

sed -E "s/([0-9][0-9])(:)([0-9][0-9][0-9])/\1,\3/g" 
share|improve this question
    
Please post your answer below as a real answer. You can then even accept it later. – slhck Oct 2 '12 at 12:25

Tell sed to replace a colon that is not followed by any more colons:

sed 's/:\([^:]\+\)$/,\1/'
share|improve this answer
    
Works, but only for the last timing. I get HH:MM:SS:XXX --> HH:MM:SS,XX (first colon remains unchanged) – leoleo Oct 2 '12 at 11:01

Replacing the third and sixth occurrence of colon on lines containing --> works here:

sed '/-->/ { s/:/,/6; s/:/,/3 }'

Do it in reverse so the occurrence order is kept.

share|improve this answer

Replace anything that matches colon+digits+end-of-line into command+what-was-found

sed  's/:([0-9]*)$/,\1/' file
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .