繁体   English   中英

我怎样才能递归地grep,但只能在具有某些扩展名的文件中?

[英]How can I grep recursively, but only in files with certain extensions?

我正在编写一个脚本来grep某些目录:

{ grep -r -i CP_Image ~/path1/;
grep -r -i CP_Image ~/path2/;
grep -r -i CP_Image ~/path3/;
grep -r -i CP_Image ~/path4/;
grep -r -i CP_Image ~/path5/; }
| mailx -s GREP email@domain.example

如何将结果限制为扩展名.h.cpp

只需使用--include参数,如下所示:

grep -inr --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.example

那应该做你想做的事。

从下面的 HoldOffHunger 答案中获取解释:

  • grep : 命令

  • -r :递归

  • -i : 忽略大小写

  • -n :每个输出行前面都有其在文件中的相对行号

  • --include \*.cpp : 所有 *.cpp: C++ 文件(使用 \ 转义,以防万一您的目录中文件名中带有星号)

  • ./ :从当前目录开始。

其中一些答案似乎语法过于繁重,或者它们在我的 Debian 服务器上产生了问题。 这对我来说非常有效:

grep -r --include=\*.txt 'searchterm' ./

...或不区分大小写的版本...

grep -r -i --include=\*.txt 'searchterm' ./
  • grep : 命令

  • -r :递归

  • -i : 忽略大小写

  • --include : all *.txt: 文本文件(用 \ 转义,以防万一你有一个文件名中带有星号的目录)

  • 'searchterm' : 搜索什么

  • ./ :从当前目录开始。

资料来源: PHP 革命:如何在 Linux 中 Grep 文件,但仅限某些文件扩展名?

grep -rnw "some thing to grep" --include=*.{module,inc,php,js,css,html,htm} ./

利用:

find . -name '*.h' -o -name '*.cpp' -exec grep "CP_Image" {} \; -print

HP 和 Sun 服务器上没有任何-r选项,但这种方式在我的 HP 服务器上对我有用:

find . -name "*.c" | xargs grep -i "my great text"

-i用于不区分大小写的字符串搜索。

由于这是查找文件的问题,所以让我们使用find

使用 GNU find 您可以使用-regex选项在扩展名为.h.cpp的目录树中查找这些文件:

find -type f -regex ".*\.\(h\|cpp\)"
#            ^^^^^^^^^^^^^^^^^^^^^^^

然后,只需对每个结果执行grep即可:

find -type f -regex ".*\.\(h\|cpp\)" -exec grep "your pattern" {} +

如果你没有这个 find 分布,你必须使用像Amir Afghani's这样的方法,使用-o来连接选项(名称以.h.cpp结尾):

find -type f \( -name '*.h' -o -name '*.cpp' \) -exec grep "your pattern" {} +
#            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

如果您真的想使用grep ,请遵循--include指示的语法:

grep "your pattern" -r --include=*.{cpp,h}
#                      ^^^^^^^^^^^^^^^^^^^

这个答案很好:

grep -r -i --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.example

但它可以更新为:

grep -r -i --include \*.{h,cpp} CP_Image ~/path[12345] | mailx -s GREP email@domain.example

这可以更简单。

最简单的方法是:

find . -type  f -name '*.extension' 2>/dev/null | xargs grep -i string

添加2>/dev/null以终止错误输出。

要在整个系统中包含更多文件扩展名和 grep 密码:

find / -type  f \( -name '*.conf' -o -name "*.log" -o -name "*.bak" \) 2>/dev/null |
xargs grep -i password

ag (银牌搜索者)对此有非常简单的语法

       -G --file-search-regex PATTERN
          Only search files whose names match PATTERN.

所以

ag -G *.h -G *.cpp CP_Image <path>

你应该为每个“-o -name”写“-exec grep”:

find . -name '*.h' -exec grep -Hn "CP_Image" {} \; -o -name '*.cpp' -exec grep -Hn "CP_Image" {} \;

或按 ( ) 分组

find . \( -name '*.h' -o -name '*.cpp' \) -exec grep -Hn "CP_Image" {} \;

选项“-Hn”显示文件名和行。

这是我通常用来查找.c.h文件的方法:

tree -if | grep \\.[ch]\\b | xargs -n 1 grep -H "#include"

或者,如果您还需要行号:

tree -if | grep \\.[ch]\\b | xargs -n 1 grep -nH "#include"

如果你想从另一个命令的输出中过滤掉扩展,例如“git”:

files=$(git diff --name-only --diff-filter=d origin/master... | grep -E '\.cpp$|\.h$')

for file in $files; do
    echo "$file"
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM