我需要一个星号追加到一条线,但只有当上述线路的前面和后面的空行(FYI,表示空行不会对他们有任何空白).
假设我有以下文件:
foo
foo
foo
foo
foo
Run Code Online (Sandbox Code Playgroud)
我希望输出看起来像这样:
foo
foo
foo
foo*
foo
Run Code Online (Sandbox Code Playgroud)
我尝试修改以下awk命令(在此处找到):
awk 'NR==1 {l=$0; next}
/^$/ {gsub(/test/,"xxx", l)}
{print l; l=$0}
END {print l}' file
Run Code Online (Sandbox Code Playgroud)
为了适应我的用途,但所有人都被束缚了.
当然,Sed或Perl解决方案也是受欢迎的!
更新:
事实证明,我问的问题并不完全正确.我真正需要的是将文本附加到非空白行的代码,这些行不是以空格开头并且后面跟着,两行向下,非空行也不是以空格开头.
对于此修订的问题,假设我有以下文件:
foo
third line foo
fifth line foo
this line starts with a space foo
this line starts with a space foo
ninth line foo
eleventh line foo
this line starts with a space foo
last line foo
Run Code Online (Sandbox Code Playgroud)
我希望输出看起来像这样:
foobar
third line …Run Code Online (Sandbox Code Playgroud)