Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

I have a series of srt files (film subtitles) that some of their time-lines overlap each others on the video! This means that some of time-lines are displayed about 1-2 seconds longer than they are in the film and conflict with the next ones!

for example:

00:00:01.000 --> 00:00:07.000
The following content is provided

00:00:04.000 --> 00:00:10.000
under a Creative Commons license.

'07' overlaps '04'!

I want to overwrite the first part of time-lines on the 2nd part of their previous line. Be care that all time-lines do not interfere with each other. some of them are correct and shorter than their next timeline! The interference is only to some of them.

share|improve this question
I don't understand what you mean by "If B Do nothing". – geirha Aug 15 '13 at 18:44
and that's what the code already does. Could you show an example where it doesn't? – geirha Aug 15 '13 at 19:42

1 Answer 1

I asked this question on #awk IRC an @geirha wrote the below great scripts. The script may be usable for other people. Subtitles overlapping problem is common because of Human error during subtitling a movie!

Suppose your timelines are in the below format:
A --> B
C --> D

Replacing B with C:

gawk '
    RS = "";
    OFS = FS = "\n";
    n = split($0, prev_rec);
    split($2, prev_time, / --> /);
    split($2, a, / --> /);
    if (a[1] < prev_time[2])
      prev_rec[2] = prev_time[1]" --> "a[1];
    for (i=1;i<=n;i++)
      print prev_rec[i];
    n = split($0, prev_rec);
    split($2, prev_time, / --> /)
  END {
  }' >

The above code compare B and C in this way:

If B > C => then run that charming command to replace B.
If B < C => Do nothing!

Replacing C with B:

gawk '
    split($2,a,/ --> /);
      (a[1] < prev) $2=prev" --> "a[2];
      print $0"\n"; prev=a[2]
  }' >

To use scripts on some amount of srt files in a folder:

for file in *.srt
  do xxx "$file" > "$file.tmp" && mv "$file.tmp" "$file";

Replace xxx with the right script codes!

share|improve this answer

Your Answer


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.