grep,但只有特定的文件扩展名

本文翻译自:grep, but only certain file extensions

I am working on writing some scripts to grep certain directories, but these directories contain all sorts of file types. 我正在编写一些脚本来grep某些目录,但这些目录包含各种文件类型。

I want to grep just .h and .cpp for now, but maybe a few others in the future. 我想grep只是.h.cpp现在,但也许其他几个人的未来。

So far I have: 到目前为止,我有:

{ 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 protected]

Can anyone show me how I would now add just the specific file extensions? 任何人都可以告诉我如何添加特定的文件扩展名?


#1楼

参考:https://stackoom.com/question/qWE5/grep-但只有特定的文件扩展名


#2楼

怎么样:

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

#3楼

Just use the --include parameter, like this: 只需使用--include参数,如下所示:

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

that should do what you want. 应该做你想要的。

Syntax notes: 语法说明:

  • -r - search recursively -r - 递归搜索
  • -i - case- insensitive search -i - 不区分大小写的搜索
  • --include=\\*.${file_extension} - search files that match the extension(s) or file pattern only --include=\\*.${file_extension} - 仅搜索与扩展名或文件模式匹配的文件

#4楼

Should write "-exec grep " for each "-o -name " 应该为每个“-o -name”写“-exec grep”

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

Or group them by ( ) 或者按()分组

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

option '-Hn' show the file name and line. 选项'-Hn'显示文件名和行。


#5楼

I am aware this question is a bit dated, but I would like to share the method I normally use to find .c and .h files: 我知道这个问题有点过时,但我想分享我通常用来查找.c.h文件的方法:

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

or if you need the line number as well: 或者如果您还需要行号:

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

#6楼

grep -rnw "some thing to grep" --include=*.{module,inc,php,js,css,html,htm} ./
发布了0 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105365661