在每個空白行中新增特定文本

在每個空白行中新增特定文本

我有一個如下文件

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

相關內容