Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自动忽略grep中的文件_Grep_Bash - Fatal编程技术网

自动忽略grep中的文件

自动忽略grep中的文件,grep,bash,Grep,Bash,有没有什么方法可以让我在搜索某个东西时使用grep忽略某些文件,相当于svnignore或gitinore?我通常在搜索源代码时使用类似的方法 grep -r something * | grep -v ignore_file1 | grep -v ignore_file2 即使我可以为grep设置一个别名来忽略这些文件也很好 find . -path ./ignore -prune -o -exec grep -r something {} \; 这样做的目的是查找当前目录中的所有文件,不

有没有什么方法可以让我在搜索某个东西时使用grep忽略某些文件,相当于svnignore或gitinore?我通常在搜索源代码时使用类似的方法

grep -r something * | grep -v ignore_file1 | grep -v ignore_file2
即使我可以为grep设置一个别名来忽略这些文件也很好

find . -path ./ignore -prune -o -exec grep -r something {} \;

这样做的目的是查找当前目录中的所有文件,不包括名为“ignore”的目录(或文件),然后对在未忽略的文件中找到的每个文件执行命令grep-r something。

我认为grep没有文件名过滤。 要完成您试图完成的任务,您可以组合使用find、xargs和grep命令。 我的记忆力不好,因此示例可能不起作用:

find -name "foo" | xargs grep "pattern"
Find是灵活的,您可以使用通配符、忽略大小写或使用正则表达式。 您可能需要阅读手册页面的完整描述

阅读下一篇文章后,显然grep确实有文件名过滤功能。

--grep上的exclude
选项也会起作用:

grep  perl * --exclude=try* --exclude=tk*

这将在当前目录中的文件中搜索perl,不包括以
try
tk
开头的文件。您可能还想看看,在许多其他功能中,默认情况下,哪些功能不会搜索.svn和.git等VCS目录。

这里是.gitignore的最低版本。需要标准UTIL:awk、sed(因为我的awk太差劲了),egrep:

grepignore
构建了一个简单的替换子句:

egrep -v ignorefile_one|ignorefile_two
效率不高,但适合手动使用

使用外壳扩展

shopt -s extglob
for file in !(file1_ignore|file2_ignore) 
do
  grep ..... "$file"
done

这有助于我的需要,-exclude和--exclude dir这基本上就是我所做的--除了我把它转换成我的bin目录中的脚本,所以几乎不需要键入。我为grep创建了一个别名--exclude和exclude dir,还有很多其他带有颜色的选项,使我的grep更容易now@ennuikiller-$alias grep='grep-T--color--exclude=ignore_file*--exclude dir=ignore dir*'尝试了一些别名和函数派生,最后得到了我在下面发布的一行代码。好主意!这是对我的外壳的新添加!您可能还想签出bash的GLOBIGNORE变量。这使shell可以细化“*”的含义。当然,这对grep-r没有帮助,但它是一个方便的工具,可以放在你的工具箱里。
shopt -s extglob
for file in !(file1_ignore|file2_ignore) 
do
  grep ..... "$file"
done