我有以下文件:
#!example
#!toto
example
#example
;example
toto
example
我想删除包含字符串的行,但以 ."example"开头的行除外"#!"。
所以结果文件应该是:
#!example
#!toto
toto
如何仅使用sed命令来做到这一点?
这条线怎么样:
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
编辑
赛德:
starting with #!,停止处理,转到下一行 ( n)#!),检查是否包含example,如果是,d删除(不要在输出中打印)awk
#!或 ( ||) 开头的不包含示例。表现:
我真的说不出来。因为需求很简单。您可以建立一个巨大的文件,并用于time自己比较。
如果您只想保留以#!
$ sed '/#!/!d' foo.sh
#!example
#!toto
这可能对您有用(GNU sed):
sed '/#!/b;/example/d' file
这将打印所有包含的行#!和所有其他行,但包含example.