我怎样才能让 grep 不打印出“没有这样的文件或目录”?错误?
我正在 grep 一大堆由 git 管理的代码,每当我执行 grep 时,我都会看到成堆的消息,其形式如下:
> grep pattern * -R -n
whatever/.git/svn: No such file or directory
有什么方法可以让这些行消失吗?
I'm grepping through a large pile of code managed by git, and whenever I do a grep, I see piles and piles of messages of the form:
> grep pattern * -R -n
whatever/.git/svn: No such file or directory
Is there any way I can make those lines go away?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您可以使用
-s
或--no-messages
标志来抑制错误。You can use the
-s
or--no-messages
flag to suppress errors.如果您要通过 git 存储库进行 grep,我建议您使用 git grep。您不需要传入
-R
或路径。这将显示当前目录下的所有匹配项。
If you are grepping through a git repository, I'd recommend you use
git grep
. You don't need to pass in-R
or the path.That will show all matches from your current directory down.
类似的错误通常会发送到“标准错误”流,您可以通过管道将其传输到文件或在大多数命令中使其消失:
Errors like that are usually sent to the "standard error" stream, which you can pipe to a file or just make disappear on most commands:
我已经看到这种情况发生了好几次,当链接断开(指向不存在的文件的符号链接)时,grep 尝试搜索不存在的目标文件(因此出现正确且准确的错误消息)。
我通常不会在通过控制台执行系统管理任务时打扰,但从脚本中我确实使用“find”查找文本文件,然后 grep 每个文件:
而不是:
I have seen that happening several times, with broken links (symlinks that point to files that do not exist), grep tries to search on the target file, which does not exist (hence the correct and accurate error message).
I normally don't bother while doing sysadmin tasks over the console, but from within scripts I do look for text files with "find", and then grep each one:
Instead of:
我通常不让 grep 自己进行递归。通常有一些目录你想跳过(.git,.svn ...)
你可以用这样的立场做聪明的别名:
乍一看似乎有点过分,但当你需要过滤掉一些模式时,它是相当方便。
I usually don't let grep do the recursion itself. There are usually a few directories you want to skip (.git, .svn...)
You can do clever aliases with stances like that one:
It may seem overkill at first glance, but when you need to filter out some patterns it is quite handy.
您是否尝试过 xargs 中的
-0
选项?像这样的东西:Have you tried the
-0
option in xargs? Something like this:在 grep 中使用
-I
。示例:
grep SEARCH_ME -Irs ~/logs
。Use
-I
in grep.Example:
grep SEARCH_ME -Irs ~/logs
.我将
stderr
重定向到stdout
,然后使用 grep 的反向匹配 (-v
) 来排除我想要隐藏的警告/错误字符串:I redirect
stderr
tostdout
and then use grep's invert-match (-v
) to exclude the warning/error string that I want to hide:我在 Windows 上的 Emacs 中运行“Mx rgrep”时遇到了很多这样的错误,并且路径中有 /Git/usr/bin 。显然在这种情况下, Mx rgrep 使用“NUL”(Windows 空设备)而不是“/dev” /空”。我通过将其添加到 .emacs 解决了该问题:
I was getting lots of these errors running "M-x rgrep" from Emacs on Windows with /Git/usr/bin in my PATH. Apparently in that case, M-x rgrep uses "NUL" (the Windows null device) rather than "/dev/null". I fixed the issue by adding this to .emacs:
让 grep 始终返回零状态的一种简单方法是使用
|| true
如您所见,这里的输出值为 0(成功)
One easy way to make grep return zero status all the time is to use
|| true
As you can see the output value here is 0 (Success)
很多答案,但没有一个有效,叹息
它不起作用:
Many answers, but none works, sigh
It does not work:
问题:
这让我抓狂。我在(Google)Sun下尝试了所有方法,但这个
grep
没有任何效果,它只是重复出现有关“sysctl:读取密钥...的错误" 在最终打印匹配之前:sudo sysctl -a | grep vm.min_free_kbytes
解决方案:
没有任何效果,直到我顿悟:如果我在前面而不是后面进行过滤会怎样?... 是的:有效:
sysctl -a --ignore 2>/dev/null | -a --ignore 2>/dev/null | grep vm.min_free_kbytes
结论:
显然不是每个命令都会有
--ignore
开关,但这是我如何解决BEFORE我的过滤问题的一个例子grep
。别太狭隘了,你追着尾巴去追求一些行不通的东西;-)Problem:
This drove me bananas. I tried everything under the (Google) Sun and nothing worked with this
grep
which just puked repeated errors about "sysctl: reading key ..." before finally printing the match:sudo sysctl -a | grep vm.min_free_kbytes
Solution:
Nothing worked UNTIL I had an epiphany: What if I filtered in the front rather than at the back?... Yup: that worked:
sysctl -a --ignore 2>/dev/null | grep vm.min_free_kbytes
Conclusion:
Obviously not every command will have the
--ignore
switch, but it's an example of how I got around the problem filtering BEFORE mygrep
. Don't get so blinkered you chase your tail pursuing something that won't work ;-)