Using Raku (formerly known as Perl_6)
~$ raku -ne '.put unless /^ 984 $/ fff *.chars == 0 ;' file
#OR
~$ raku -ne '.put unless /^ 984 $/ fff {.chars == 0} ;' file
The above code uses Raku's fff
"flip-flop" operator, which detects a record starting with 984
and ending on a blank line (.chars
equal zero). Note the above code makes no attempt to detect a blank line before 984
.
Sample Input:
982
01:25:09,473 --> 01:25:10,978
Stay with me.
983
01:25:09,473 --> 01:25:10,978
Stay with me.
984
01:25:15,390 --> 01:25:18,484
( MAJESTIC MUSIC )
985
01:25:18,485 --> 01:25:18,500
( END CREDITS )
Sample Output (1):
982
01:25:09,473 --> 01:25:10,978
Stay with me.
983
01:25:09,473 --> 01:25:10,978
Stay with me.
985
01:25:18,485 --> 01:25:18,500
( END CREDITS )
Raku provides a number of fff
variants to leave either-or-both of the two recognition sequences in the return. They are ^fff
or fff^
or ^fff^
. This reduces the need to use lookaheads/lookbehinds. For example, simply change fff
in the above code to ^fff^
to get the following return:
Sample Output (2):
982
01:25:09,473 --> 01:25:10,978
Stay with me.
983
01:25:09,473 --> 01:25:10,978
Stay with me.
984
985
01:25:18,485 --> 01:25:18,500
( END CREDITS )
If you want/need to separate records first, slurp
the file in all at once and simply split on \n\n
consecutive newlines. Then the remainder of the code simplifies to the following, but unfortunately adds two blank lines to the very end of the file:
~$ raku -e 'for slurp.split("\n\n") { put $_ ~ "\n" unless /^984 / };' file
Not to worry, see the first link below to use Raku for removing blank lines at the beginning/end of a file.
https://unix.stackexchange.com/a/725227/227738
https://docs.raku.org
https://raku.org
984
regardless of whether they are last or not? – Kusalananda Mar 05 '23 at 16:55984
, or other such records of your choice? Or do you want to retain the top line of each record (e.g.984
) as a "title", and then delete the "body" of the record only? Such a method would leave all record numbers sequentially intact (982, 983, 984...). – jubilatious1 Mar 21 '23 at 19:16libsubtitles-perl
) description:This module provides means for simple loading, re-timing, and storing these subtitle files. The module supports srt, sub, smi subtitle formats. A command-line tool 'subs' for manipulation of subtitles files is included in this package.
BTW, see also https://metacpan.org/pod/App::SubtitleUtils – cas Mar 30 '23 at 11:14