如何找出当前目录中不包含单词foo(使用grep)的文件?
当前回答
我的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 ">"
其他回答
您可以单独使用grep(没有find)来完成。
grep -riL "foo" .
这是对grep上使用的参数的解释
-L, --files-without-match
each file processed.
-R, -r, --recursive
Recursively search subdirectories listed.
-i, --ignore-case
Perform case insensitive matching.
如果你使用l(小写),你将得到相反的(文件匹配)
-l, --files-with-matches
Only the names of files containing selected lines are written
当grep没有-L选项时(例如IBM AIX),只有grep和shell时的另一种选择:
for file in * ; do grep -q 'my_pattern' $file || echo $file ; done
你实际上需要:
find . -not -ipath '.*svn*' -exec grep -H -E -o -c "foo" {} \; | grep :0\$
为了完整,ripgrep版本如下:
rg --files-without-match "pattern"
你可以结合文件类型和搜索路径,例如:
rg --files-without-match -t ruby "frozen_string_literal: true" app/
我运气很好
grep -H -E -o -c "foo" */*/*.ext | grep ext:0
我用grep -v的尝试只是给了我所有没有“foo”的行。
推荐文章
- 在Windows中有像GREP这样的模式匹配实用程序吗?
- 如何在Python中获得所有直接子目录
- 即使模板文件存在,Flask也会引发TemplateNotFound错误
- 如何从命令行将每两行合并为一行?
- 如何复制在bash所有目录和文件递归?
- 如何在Ruby中创建文件
- __FILE__宏显示完整路径
- 匹配前后的Grep字符?
- (grep)正则表达式匹配非ascii字符?
- 如何从另一个文件A中删除文件B中出现的行?
- 如何使用查找命令从列表中查找所有具有扩展名的文件?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- 如何删除文件中的特定行?
- 使用Java重命名文件
- PowerShell等价于grep -f