为什么两次使用 grep 时没有显示输出? [英] Why no output is shown when using grep twice?
问题描述
基本上我想知道为什么这不输出任何内容:
Basically I'm wondering why this doesn't output anything:
tail --follow=name file.txt | grep something | grep something_else
你可以假设它应该产生输出我已经运行了另一行来确认
You can assume that it should produce output I have run another line to confirm
cat file.txt | grep something | grep something_else
看起来你不能多次通过管道输出 tail !?任何人都知道交易是什么,有解决方案吗?
It seems like you can't pipe the output of tail more than once!? Anyone know what the deal is and is there a solution?
为了回答到目前为止的问题,该文件肯定有应该由 grep 显示的内容.作为grep是否像这样完成的证据:
To answer the questions so far, the file definitely has contents that should be displayed by the grep. As evidence if the grep is done like so:
tail --follow=name file.txt | grep something
输出正确显示,但如果使用它:
Output shows up correctly, but if this is used instead:
tail --follow=name file.txt | grep something | grep something
没有显示输出.
如果有帮助,我正在运行 ubuntu 10.04
If at all helpful I am running ubuntu 10.04
推荐答案
在管道内部时,您可能还会遇到 grep 缓冲问题.即,您看不到
You might also run into a problem with grep buffering when inside a pipe. ie, you don't see the output from
tail --follow=name file.txt | grep something > output.txt
因为 grep 会缓冲它自己的输出.
since grep will buffer its own output.
使用 grep 的 --line-buffered 开关来解决这个问题:
Use the --line-buffered switch for grep to work around this:
tail --follow=name file.txt | grep --line-buffered something > output.txt
如果您想尽快将后续结果放入 output.txt 文件中,这将非常有用.
This is useful if you want to get the results of the follow into the output.txt file as rapidly as possible.
这篇关于为什么两次使用 grep 时没有显示输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!