-1

This better goes with an example (yes, from an .srt-file):

231
00:13:35,230 --> 00:13:37,120
- Oh, my sister got me into it.

232
00:13:37,129 --> 00:13:38,269
- Yeah?

233
00:13:37,129 --> 00:13:38,269
Is that her?

234
00:13:40,049 --> 00:13:41,090
- Yeah.

The line 00:13:37,129 --> 00:13:38,269 appears twice and I want to connect the two sections. So it would have to work like this:

  • check all lines containing " --> "
  • if it matches the previous such finding remove this line and the two lines above

So the result would be:

231
00:13:35,230 --> 00:13:37,120
- Oh, my sister got me into it.

232
00:13:37,129 --> 00:13:38,269
- Yeah?
Is that her?

234
00:13:40,049 --> 00:13:41,090
- Yeah.

This goes way beyond my sed-skills. It probably works with that internal buffer and pattern space? Well, I don't even have a clue how to approach this...

  • Why not awk but sed? – oguz ismail Oct 20 at 17:47
  • @oguzismail Well, I can't do backward things in awk, can I? I mean I don't care actually. – sjngm Oct 20 at 18:24
  • @oguzismail Oh, whow, cool. Don't be shy, make that an answer :) – sjngm Oct 20 at 18:52
  • Great, who downvoted my question and why? – sjngm Oct 21 at 15:15
4

I'd use awk for that:

$ cat tst.awk
(!NF) {                # blank line
    b = ""; f = 1      # empty buffer, start buffering
}
/-->/ {                # timestamp
    f = 0              # stop buffering
    if (p == $0) {     # same timestamp
        next           # discard buffer, start over
    }
    p = $0             # save timestamp
    printf "%s", b     # print buffer
}
f {                    # buffering enabled
    b = (b $0 ORS)     # buffer line
    next               # start over
}
1                      # print line

outputs:

$ awk -f tst.awk file
231
00:13:35,230 --> 00:13:37,120
- Oh, my sister got me into it.

232
00:13:37,129 --> 00:13:38,269
- Yeah?
Is that her?

234
00:13:40,049 --> 00:13:41,090
- Yeah.
0

I think the awk version is much better, but here is a bash version just for fun : )

out="";
while read line;
do
    if [ "$prevtime" != "$line" ];then
        out="${out}${line}\n";
    else
        out="$(echo -e "${out}"|head -n -2)\n";
    fi ;
    echo  "${line}" |grep -q  "\-\->" &&  prevtime=$line  ;
done <test.srt ; echo -e "$out"

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.