Linux 从 grep 中排除 .svn 目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1491514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:44:15  来源:igfitidea点击:

Exclude .svn directories from grep

linuxbashsvngrep

提问by Kees Kist

When I grep my Subversion working copy directory, the results include a lot of files from the .svn directories. Is it possible to recursively grep a directory, but exclude all results from .svn directories?

当我 grep 我的 Subversion 工作副本目录时,结果包括 .svn 目录中的许多文件。是否可以递归地 grep 目录,但排除 .svn 目录中的所有结果?

采纳答案by psychoschlumpf

If you have GNU Grep, it should work like this:

如果你有 GNU Grep,它应该像这样工作:

grep --exclude-dir=".svn"

If happen to be on a Unix System without GNU Grep, try the following:

如果碰巧在没有 GNU Grep 的 Unix 系统上,请尝试以下操作:

grep -R "whatever you like" *|grep -v "\.svn/*" 

回答by Brian Agnew

If you use ack(a 'better grep') it will handle this automatically (and do a lot of other clever things too!). It's well worth checking out.

如果您使用ack(一个“更好的 grep”),它会自动处理(并且还可以做很多其他聪明的事情!)。值得一试。

回答by Patrice Bernassola

I think the --exclude option of recursion is what you are searching for.

我认为递归的 --exclude 选项是您正在寻找的。

回答by karatedog

grep --exclude-dir=".svn"

works because the name ".svn" is rather unique. But this might fail on a more generalized name.

之所以有效,是因为名称“.svn”相当独特。但这可能会在更通用的名称上失败。

grep --exclude-dir="work"

is not bulletproof, if you have "/home/user/work" and "/home/user/stuff/work" it will skip both. It is not possible to define "/*/work/*" to restrict the exclusion to only the former folder name. As far as I could experiment, in GNU grep the simple --exclude won't exclude directories.

不是防弹的,如果您有“/home/user/work”和“/home/user/stuff/work”,它将跳过两者。无法定义“/*/work/*”以将排除限制为仅前一个文件夹名称。据我所知,在 GNU grep 中,简单的 --exclude 不会排除目录。

回答by Joseph Huttner

Two greps will do the trick:

两个 grep 可以解决问题:

  1. The first grep will get everything.
  2. The second grep will use output of first grep as input (via piping). By using the -v flag, grep will select the lines which DON'T match the search terms. Voila. You are left with all the ouputs from the first grep which do not contain .svn in the filepath.

    -v, --invert-match Invert the sense of matching, to select non-matching lines.

    grep the_text_you_want_to_search_for * | grep -v .svn
    
  1. 第一个 grep 将获得一切。
  2. 第二个 grep 将使用第一个 grep 的输出作为输入(通过管道)。通过使用 -v 标志,grep 将选择与搜索词不匹配的行。瞧。您将获得第一个 grep 的所有输出,这些输出在文件路径中不包含 .svn。

    -v, --invert-match 反转匹配的意义,选择不匹配的行。

    grep the_text_you_want_to_search_for * | grep -v .svn
    

回答by Max Cantor

psychoschlumpf is correct, but it only works if you have the latest version of grep. Earlier versions do not have the --exclude-diroption. However, if you have a very large codebase, double-grep-ing can take forever. Drop this in your .bashrcfor a portable .svn-less grep:

Psychoschlumpf 是正确的,但只有在您拥有最新版本的grep. 早期版本没有该--exclude-dir选项。但是,如果您有一个非常大的代码库,则双重化grep可能需要很长时间。将其放入您.bashrc的便携式.svn无grep:

alias sgrep='find . -path "*/.svn" -prune -o -print0 | xargs -0 grep'

Now you can do this:

现在你可以这样做:

sgrep some_var

... and get expected results.

...并获得预期的结果。

Of course, if you're an insane person like me who just hasto use the same .bashrceverywhere, you could spend 4 hours writing an overcomplicated bash function to put there instead. Or, you could just wait for an insane person like me to post it online:

当然,如果你是一个疯狂的人喜欢我,谁只是拥有使用相同的.bashrc无处不在,你可以花费40小时的时间的过于复杂的bash函数,而不是放在那里。 或者,您可以等待像我这样的疯子将其发布到网上:

http://gist.github.com/573928

http://gist.github.com/573928

回答by osgx

For grep >=2.5.1a

对于 grep >=2.5.1a

You can put this into your environment (e.g. .bashrc)

你可以把它放到你的环境中(例如.bashrc

export GREP_OPTIONS='--exclude-dir=".svn"'

PS: thanks to Adrinan, there are extra quotes in my version:

PS:感谢 Adrinan,在我的版本中有额外的引用:

export GREP_OPTIONS='--exclude-dir=.svn'

回答by Sid Sarasvati

I tried double grep'in on my huge code base and it took forever so I got this solution with the help of my co-worker

我在我庞大的代码库上尝试了双重 grep'in 并且花了很长时间所以我在我的同事的帮助下得到了这个解决方案

Pruning is much faster as it stops find from processing those directories compared to 'grep -v' which processes everything and only excludes displaying results

与处理所有内容且仅排除显示结果的“grep -v”相比,修剪速度要快得多,因为它停止 find 处理这些目录

find . -name .svn -prune -o -type f -print0 | xargs -0 egrep 'YOUR STRING'

You can also alias this command in your .bashrc as

您还可以在 .bashrc 中将此命令别名为

alias sgrep='find . -name .svn build -prune -o -type f -print0 | xargs -0 egrep '

Now simply use

现在只需使用

sgrep 'whatever' 

回答by ZaSter

For grep version 2.5.1 you can add multiple --excludeitems to filter out the .svnfiles.

对于 grep 2.5.1 版,您可以添加多个--exclude项目来过滤.svn文件。

$ grep -V | grep grep
grep (GNU grep) 2.5.1

GREP_OPTIONS="--exclude=*.svn-base --exclude=entries --exclude=all-wcprops" grep -l -R  whatever ./

回答by quickshiftin

Another option, albeit one that may not be perceived as an acceptableanswer is to clone the repo into git and use git grep.

另一种选择(尽管可能不被视为可接受的答案)是将 repo 克隆到 git 并使用git grep.

Rarely, I run into svn repositories that are so massive, it's just impractical to clone via git-svn. In these rare cases, I use a double grep solution, svngrep, but as many answers here indicate, this could be slow on large repositories, and exclude '.svn' occurrences that aren't directories. I would argue that these would be extremely seldom though...

很少,我会遇到如此庞大的 svn 存储库,通过git-svn. 在这些罕见的情况下,我使用双 grep 解决方案svngrep,但正如这里的许多答案所表明的那样,这在大型存储库上可能会很慢,并排除不是目录的“.svn”出现。我会争辩说,这些将是非常罕见的......

Also regarding slow performance of multiple greps, once you've used something like git, pretty much everything seems slow in svn!

另外关于多个 grep 的缓慢性能,一旦你使用过像 git 这样的东西,在 svn 中几乎所有东西看起来都很慢!

One last thing.., my variation of svngrep passes through colorization, beware, the implementation is ugly! Roughly grep -rn "$what" $where | egrep -v "$ignore" | grep --color "$what"

最后一件事..,我的 svngrep 变体通过着色,当心,实现是丑陋的!大致grep -rn "$what" $where | egrep -v "$ignore" | grep --color "$what"