2

I have such confused ideas I couldn't describe well the problem synthetically, hence the title is horrible. Suggest me a better one too, while you're at it.

Basically, I have two SubRip (.srt) subtitles with the same number of lines. They are in different languages and I need to sync them.

Point syncing one to the other doesn't give a good enough result, because the second one apparently have some bad timed lines. What I want to do is copying timestamps from the first to the second.

Given that some lines have a line break, the timings are not cadenced with a fixed pattern. To get the lines with the timings, I should look for the arrow going from start time to end time.

1.srt

1
00:00:01,336 --> 00:00:03,437
Foo

2
00:00:03,440 --> 00:00:05,093
Bar
Baz

3
00:00:05,096 --> 00:00:07,330
Qux

2.srt

1
00:00:01,336 --> 00:00:03,935
Quux

2
00:00:03,936 --> 00:00:05,201
Garply
Waldo

3
00:00:05,803 --> 00:00:07,467
Fred
more 1.srt | grep -- "-->"

00:00:01,336 --> 00:00:03,437
00:00:03,440 --> 00:00:05,093
00:00:05,096 --> 00:00:07,330

more 2.srt  | grep -- "-->"

00:00:01,336 --> 00:00:03,935
00:00:03,936 --> 00:00:05,201
00:00:05,803 --> 00:00:07,467

How can I replace the lines containing the timestamps in 1.srt with the corresponding ones of 2.srt?

2

Let's name the files descriptively: good-text and good-times.

  1. Isolate lines with timestamps from good-times:

    grep -- '-->' good-times > the-ringer
    
  2. Let awk replace wrong timestamps:

    < good-text awk '
       { if ($2 == "-->") getline < "the-ringer"
       print }
    ' > good-all
    

    Each time awk encounters a line where the second field is --> it replaces it with a line from the-ringer.

  3. Get rid of Walter's dirty undies garbage: rm the-ringer.

Examine good-all, it should contain what you want. The whole procedure could be done solely with awk but I decided to KISS.

  • Can't give enough upvotes for the Lebowski reference. And, as an Arch Linux user, the KISS principle is very dear to me. Thank you, and a good day to you, sir! – Jeffrey Lebowski May 11 '20 at 19:29

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.