使用 grep --exclude/--include 语法不通过某些文件进行 grep [英] Use grep --exclude/--include syntax to not grep through certain files

查看:43
本文介绍了使用 grep --exclude/--include 语法不通过某些文件进行 grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在目录树中的文本文件中查找字符串 foo=.它在一台普通的 Linux 机器上,我有 bash shell:

I'm looking for the string foo= in text files in a directory tree. It's on a common Linux machine, I have bash shell:

grep -ircl "foo=" *

目录中还有许多与"foo="匹配的二进制文件.由于这些结果不相关并且会减慢搜索速度,我希望 grep 跳过搜索这些文件(主要是 JPEG 和 PNG 图像).我该怎么做?

In the directories are also many binary files which match "foo=". As these results are not relevant and slow down the search, I want grep to skip searching these files (mostly JPEG and PNG images). How would I do that?

我知道有 --exclude=PATTERN--include=PATTERN 选项,但是模式格式是什么?grep 的手册页说:

I know there are the --exclude=PATTERN and --include=PATTERN options, but what is the pattern format? The man page of grep says:

--include=PATTERN     Recurse in directories only searching file matching PATTERN.
--exclude=PATTERN     Recurse in directories skip file matching PATTERN.

搜索 grep includegrep include excludegrep exclude 和变体没有找到任何相关内容

Searching on grep include, grep include exclude, grep exclude and variants did not find anything relevant

如果有更好的方法只在某些文件中进行 grepping,我完全赞成;移动有问题的文件不是一种选择.我不能只搜索某些目录(目录结构一团糟,到处都是).此外,我无法安装任何东西,所以我必须使用常用工具(例如 grep 或建议的 find).

If there's a better way of grepping only in certain files, I'm all for it; moving the offending files is not an option. I can't search only certain directories (the directory structure is a big mess, with everything everywhere). Also, I can't install anything, so I have to do with common tools (like grep or the suggested find).

推荐答案

使用 shell 通配语法:

grep pattern -r --include=*.cpp --include=*.h rootdir

--exclude 的语法是相同的.

注意星号用反斜杠转义以防止它被shell扩展(引用它,例如--include =*.cpp",也可以).否则,如果当前工作目录中有与模式匹配的任何文件,命令行将扩展为类似 grep pattern -r --include=foo.cpp --include=bar.cpp rootdir,它只会搜索名为 foo.cppbar.cpp 的文件,这很可能不是您想要的.

Note that the star is escaped with a backslash to prevent it from being expanded by the shell (quoting it, such as --include="*.cpp", would work just as well). Otherwise, if you had any files in the current working directory that matched the pattern, the command line would expand to something like grep pattern -r --include=foo.cpp --include=bar.cpp rootdir, which would only search files named foo.cpp and bar.cpp, which is quite likely not what you wanted.

更新 2021-03-04

我已经编辑了原始答案以删除使用 大括号扩展,这是Bash和zsh等几个shell提供的一个特性,用于简化这样的模式;但请注意,大括号扩展不符合 POSIX shell.

I've edited the original answer to remove the use of brace expansion, which is a feature provided by several shells such as Bash and zsh to simplify patterns like this; but note that brace expansion is not POSIX shell-compliant.

最初的例子是:

grep pattern -r --include=*.{cpp,h} rootdir

搜索rootdir目录下的所有.cpp.h文件.

to search through all .cpp and .h files rooted in the directory rootdir.

这篇关于使用 grep --exclude/--include 语法不通过某些文件进行 grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆