空白行ごとに特定のテキストを追加する

空白行ごとに特定のテキストを追加する

以下のようなファイルがあります

abc

pqr
xyz


aaa
bbb

ccc

以下のように、すべての空白行に「これはテストです」などの特定のテキストを追加したい

abc
this is test
pqr
xyz
this is test
this is test
aaa
bbb
this is test
ccc

これを実行するのを手伝ってください。ありがとう

答え1

空行の正規表現が であることを知っているので^$、 を使用しますsed

$ sed 's/^$/this is test/' file 
abc
this is test
pqr
xyz
this is test
this is test
aaa
bbb
this is test
ccc

を使用するとawk、要素の数を信頼できますNF。これが の場合、行を必要な文字列に0設定します。$0

$ awk '!NF{$0="this is test"}1' file

関連情報