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


whereas it should be


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


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" 
  • 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/'
  • 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.


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

sed  's/:([0-9]*)$/,\1/' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.