Mic*_*ant 14 sed awk text-processing regular-expression
我有一堆输出通过 sed 和 awk。
如何使用 START 为输出添加前缀并使用 END 为答案添加后缀?
例如,如果我有
All this code
on all these lines
and all these
Run Code Online (Sandbox Code Playgroud)
我怎么能得到:
START
All this code
on all these lines
and all these
END
Run Code Online (Sandbox Code Playgroud)
?
我的尝试是:
awk '{print "START";print;print "END"}'
Run Code Online (Sandbox Code Playgroud)
但我得到了
...
START
All this code
END
START
on all these lines
END
START
and all these
END
Run Code Online (Sandbox Code Playgroud)
Mic*_*ant 19
这是有效的,如jasonwryan 所示:
awk 'BEGIN{print "START"}; {print}; END{print "END"}'
Run Code Online (Sandbox Code Playgroud)
Sco*_*ott 12
这可以sed用
sed -e $'1i\\\nSTART' -e $'$a\\\nEND'
Run Code Online (Sandbox Code Playgroud)
1i手段我线1之前nsert; $a意味着一个最后的行之后PPEND。该$'…'语法是bash的特异性。在其他 shell 中,您应该能够使用以下命令执行此操作:
sed -e '1i\ Enter START' -e '$a\ Enter END'Enter
如果您已经在使用 sed,则可以使用1来匹配第一行和$最后一行(请参阅Scott 的回答)。如果您已经在使用 awk,则可以使用一个BEGIN块在第一行之前END运行代码,并在最后一行之后使用一个块来运行代码(请参阅Michael Durrant 的回答)。
如果您只需要添加页眉和页脚,只需使用echoand cat。
echo START
cat
echo END
Run Code Online (Sandbox Code Playgroud)
在管道中,要运行多个命令,请使用{ … }告诉解析器它们是一个单一的复合命令。
content-generator |
{ echo START; cat; echo END; } |
postprocessor
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27280 次 |
| 最近记录: |