如果模式匹配,则删除行,除非行包含字符串

时间:2015-10-02 13:49:58

标签: regex

如果匹配模式xxx,则应删除文本文件中的所有行,除非该行包含yyy

示例输入(文字):

- 1 The first line does not contain the pattern.
- 2 The second line does contain the pattern xxx but still can't be deleted because of yyy.
- 3 The third line should be remove because it only contains xxx.

所需的输出(文字):

 
- 1 The first line does not contain the pattern.
- 2 The second line does contain the pattern xxx but still can't be deleted because of yyy.

2 个答案:

答案 0 :(得分:1)

这应该有效(未经测试)

 awk '!/xxx/ || /yyy/'

答案 1 :(得分:0)

如果行号是输入/输出的一部分

$ awk '!/xxx/ || /yyy/ {$1=++c;print}' lines

1 This first line does not contain the pattern.
2 This second line does contain the pattern xxx but still can't be deleted because of yyy.

如果不需要行号,请使用@ rici的解决方案