Marcelo Marcelo - 9 days ago 5
Linux Question

How to add strings “< /font>” after lines beginning with < fc=ff00>, but not add two consecutive lines?

Consider this sample file:

01
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters.
02
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters
Alphabetic characters,
03
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters,


I'd like add strings just a single line of text.

01
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters.</font>
02
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters
Alphabetic characters,</font>
03
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters,</font>


I'm not clear how to exclude first line. Any suggestion?

Answer

This is what I think you're really trying to do:

$ cat file
01
00:00:00,000 --> 00:00:00,000
Alphabetic characters.
02
00:00:00,000 --> 00:00:00,000
Alphabetic characters
Alphabetic characters,
03
00:00:00,000 --> 00:00:00,000
Alphabetic characters,

$ cat tst.awk
/^[[:alpha:]]/ { buf = (buf ? buf RS : "") $0; next }
buf { printf "<fc=ff00>%s</font>\n", buf; buf="" }
{ print }
END { if (buf) printf "<fc=ff00>%s</font>\n", buf }

$ awk -f tst.awk file
01
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters.</font>
02
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters
Alphabetic characters,</font>
03
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters,</font>
Comments