我有以下文件:
#!example
#!toto
example
#example
;example
toto
example除了以"#!"开头的行之外,我想删除包含字符串"example"的行。
因此,结果文件应该是:
#!example
#!toto
toto如何仅使用sed命令来完成此操作?
发布于 2013-04-20 00:39:29
下面这行如何:
sed '/^#!/n;/example/d' file使用您的示例文本进行测试:
kent$ cat file
#!example
#!toto
example
#example
;example
toto
example
kent$ sed '/^#!/n;/example/d' file
#!example
#!toto
toto如果你喜欢,awk也可以这样做:
kent$ awk '/^#!/ || !/example/' file
#!example
#!toto
toto编辑
sed:
如果行与starting with #!匹配,则停止处理,转到上面的下一行检查失败(与#!不匹配),检查是否包含example,如果包含,则删除d (不在输出中打印)
awk
的
#!或(||)开头
性能:
我真的分不清。因为这个要求非常简单。您可以构建一个很大的文件,然后使用time自己进行比较。
发布于 2013-04-20 00:25:53
如果只想保留以#!开头的行
$ sed '/#!/!d' foo.sh
#!example
#!toto发布于 2013-04-20 05:58:07
这可能适用于您(GNU sed):
sed '/#!/b;/example/d' file这将打印所有包含#!的行和除example之外的所有其他行。
https://stackoverflow.com/questions/16109207
复制相似问题