使用grep进行负匹配(匹配不包含foo的行)

时间:2010-08-23 14:24:14

标签: grep

我一直试图找出这个命令的语法:

grep ! error_log | find /home/foo/public_html/ -mmin -60

grep '[^error_log]' | find /home/baumerf/public_html/ -mmin -60

我需要查看已修改的所有文件,但名为error_log的文件除外。

I've read about it here,但只找到一个not - 正则表达式。

3 个答案:

答案 0 :(得分:1497)

grep -v是你的朋友:

grep --help | grep invert  
  

-v, - 反向匹配选择不匹配的行

另请查看相关的-L-l的补充)。

  

-L, - files-without-match仅打印不包含匹配项的文件名

答案 1 :(得分:110)

您还可以将awk用于这些目的,因为它允许您以更清晰的方式执行更复杂的检查:

不包含foo的行:

awk '!/foo/'

既不包含foo也不包含bar的行:

awk '!/foo/ && !/bar/'

既不包含foo也不包含bar的行,其中包含foo2bar2

awk '!/foo/ && !/bar/ && (/foo2/ || /bar2/)'

等等。

答案 2 :(得分:11)

在你的情况下,你可能不想使用grep,而是在find命令中添加一个否定子句,例如

find /home/baumerf/public_html/ -mmin -60 -not -name error_log

如果要在名称中包含通配符,则必须将它们转义,例如:排除后缀.log:

的文件
find /home/baumerf/public_html/ -mmin -60 -not -name \*.log
相关问题