如何添加字符串&#34; <fc = ff00>&#34;在包含模式[:alpha:]的任何行之前,但不添加两个连续的行?

时间:2016-11-21 20:21:46

标签: linux bash awk sed

考虑这个示例文件:

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,

我只想在一行文字中添加字符串。

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,

使用sed:     sed&#39; / [[:alpha:]] / s / ^ //&#39;文件&gt; new_file

我不清楚如何排除第二行。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

$ awk '!f && /[[:alpha:]]/{print "<fc=ff00>" $0; f=1; next} {print; f=0}' 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,