如何找出当前目录中不包含单词foo(使用grep)的文件?


当前回答

我运气很好

grep -H -E -o -c "foo" */*/*.ext | grep ext:0

我用grep -v的尝试只是给了我所有没有“foo”的行。

其他回答

你实际上需要:

find .  -not  -ipath '.*svn*' -exec  grep  -H -E -o -c  "foo"  {} \; | grep :0\$

当grep没有-L选项时(例如IBM AIX),只有grep和shell时的另一种选择:

for file in * ; do grep -q 'my_pattern' $file || echo $file ; done

为了完整,ripgrep版本如下:

rg --files-without-match "pattern"

你可以结合文件类型和搜索路径,例如:

rg --files-without-match -t ruby "frozen_string_literal: true" app/

下面的命令可以帮助你过滤包含子字符串“foo”的行。

cat file | grep -v "foo"

我的grep没有任何-L选项。我确实找到了解决办法。

这些想法是:

将包含相应字符串的所有文件名转储到txt1.txt。 将目录中的所有文件名转储到txt2.txt文件中。 使用diff命令区分2个转储文件。 Grep 'foo' *.log | cut -c1-14 | uniq > txt1.txt Grep * *.log | cut -c1-14 | uniq > txt2.txt Diff txt1.txt txt2.txt | grep ">"