Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

3 Answers 3

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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