我有一个包含一些行的文件。
如果这些行包含一些关键字,我想在这些行中添加一个前缀。
我该怎么做?谢谢!
示例:
我想在行首带有" Hello
"的行中添加"###"
:
当前此文件具有:
This is the beginning.
Hello I am Simon.
This is a simple line;
Hello Mike.
This is another line.
我想将此文件更改为:
This is the beginning.
###Hello I am Simon.
This is a simple line;
###Hello Mike.
This is another line.
sed 程序将为您做到这一点。对于您发布的示例:
$ sed 's/^Hello/###Hello/' <file>
awk 解决方案是:
awk '{if($1=="Hello"){print "###"$0 ; } else {print $0}}' <file>
但我认为sed是这种情况的更好方法。