gho*_*g74 773
如果你的grep有-L
(或--files-without-match
)选项:
$ grep -L "foo" *
Run Code Online (Sandbox Code Playgroud)
And*_*ter 44
看看ack
.它会.svn
自动为您排除,为您提供Perl正则表达式,并且是单个Perl程序的简单下载.
相当于你正在寻找的东西,应该是ack
:
ack -L foo
Run Code Online (Sandbox Code Playgroud)
Sen*_*mar 16
以下命令为我提供了所有不包含该模式的文件foo
:
find . -not -ipath '.*svn*' -exec grep -H -E -o -c "foo" {} \; | grep 0
Run Code Online (Sandbox Code Playgroud)
use*_*305 13
以下命令不需要svn
使用秒来过滤掉文件夹grep
.
grep -rL "foo" ./* | grep -v "\.svn"
Run Code Online (Sandbox Code Playgroud)
Zak*_*Zak 13
如果您使用的是 git,这将搜索所有跟踪的文件:
git grep -L "foo"
Run Code Online (Sandbox Code Playgroud)
如果您打开了 ** 子目录通配符,您可以搜索跟踪文件的子集(shopt -s globstar
在 .bashrc 中,请参阅此内容):
git grep -L "foo" -- **/*.cpp
Run Code Online (Sandbox Code Playgroud)
Adr*_*ian 10
You can do it with grep alone (without find).
grep -riL "foo" .
Run Code Online (Sandbox Code Playgroud)
This is the explanation of the parameters used on grep
-L, --files-without-match
each file processed.
-R, -r, --recursive
Recursively search subdirectories listed.
-i, --ignore-case
Perform case insensitive matching.
Run Code Online (Sandbox Code Playgroud)
If you use l
(lowercased) you will get the opposite (files with matches)
-l, --files-with-matches
Only the names of files containing selected lines are written
Run Code Online (Sandbox Code Playgroud)
ako*_*nov 10
为了完整性,ripgrep版本:
rg --files-without-match "pattern"
Run Code Online (Sandbox Code Playgroud)
您可以结合文件类型和搜索路径,例如
rg --files-without-match -t ruby "frozen_string_literal: true" app/
Run Code Online (Sandbox Code Playgroud)
小智 9
你真的需要:
find . -not -ipath '.*svn*' -exec grep -H -E -o -c "foo" {} \; | grep :0\$
Run Code Online (Sandbox Code Playgroud)
小智 6
我好运
grep -H -E -o -c "foo" */*/*.ext | grep ext:0
Run Code Online (Sandbox Code Playgroud)
我的尝试grep -v
只给了我所有的线条而没有"foo".
问题
我需要重构一个大型项目,该项目使用.phtml
文件使用内联 PHP 代码写出 HTML。我想改用Mustache模板。我想找到任何.phtml
不包含字符串的 giles,new Mustache
因为它们仍然需要重写。
解决方案
find . -iname '*.phtml' -exec grep -H -E -o -c 'new Mustache' {} \; | grep :0$ | sed 's/..$//'
解释
管道前:
找
find .
递归查找文件,从这个目录开始
-iname '*.phtml'
文件名必须包含.phtml
(i
使其不区分大小写)
-exec 'grep -H -E -o -c 'new Mustache' {}'
grep
在每个匹配的路径上运行命令
格雷普
-H
始终使用输出行打印文件名标题。
-E
将模式解释为扩展的正则表达式(即强制 grep 表现为 egrep)。
-o
仅打印行的匹配部分。
-c
仅将选定行的计数写入标准输出。
这将为我提供以 结尾的所有文件路径的列表.phtml
,并计算字符串new Mustache
在每个路径中出现的次数。
$> find . -iname '*.phtml$' -exec 'grep -H -E -o -c 'new Mustache' {}'\;
./app/MyApp/Customer/View/Account/quickcodemanagestore.phtml:0
./app/MyApp/Customer/View/Account/studio.phtml:0
./app/MyApp/Customer/View/Account/orders.phtml:1
./app/MyApp/Customer/View/Account/banking.phtml:1
./app/MyApp/Customer/View/Account/applycomplete.phtml:1
./app/MyApp/Customer/View/Account/catalogue.phtml:1
./app/MyApp/Customer/View/Account/classadd.phtml:0
./app/MyApp/Customer/View/Account/orders-trade.phtml:0
Run Code Online (Sandbox Code Playgroud)
第一个管道grep :0$
过滤此列表以仅包含以 结尾的行:0
:
$> find . -iname '*.phtml' -exec grep -H -E -o -c 'new Mustache' {} \; | grep :0$
./app/MyApp/Customer/View/Account/quickcodemanagestore.phtml:0
./app/MyApp/Customer/View/Account/studio.phtml:0
./app/MyApp/Customer/View/Account/classadd.phtml:0
./app/MyApp/Customer/View/Account/orders-trade.phtml:0
Run Code Online (Sandbox Code Playgroud)
第二个管道sed 's/..$//'
去掉每行的最后两个字符,只留下文件路径。
$> find . -iname '*.phtml' -exec grep -H -E -o -c 'new Mustache' {} \; | grep :0$ | sed 's/..$//'
./app/MyApp/Customer/View/Account/quickcodemanagestore.phtml
./app/MyApp/Customer/View/Account/studio.phtml
./app/MyApp/Customer/View/Account/classadd.phtml
./app/MyApp/Customer/View/Account/orders-trade.phtml
Run Code Online (Sandbox Code Playgroud)