Linux 为什么两次使用 grep 时没有显示输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5427483/
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
Why no output is shown when using grep twice?
提问by radman
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
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?
似乎您不能多次通过管道输出 tail !?任何人都知道交易是什么,有解决方案吗?
EDIT: 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:
编辑:要回答到目前为止的问题,该文件肯定包含应该由 grep 显示的内容。作为证据,如果 grep 是这样完成的:
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
No output is shown.
没有显示输出。
If at all helpful I am running ubuntu 10.04
如果有帮助,我正在运行 ubuntu 10.04
采纳答案by simonc
You might also run into a problem with grep buffering when inside a pipe. ie, you don't see the output from
在管道内时,您也可能会遇到 grep 缓冲问题。即,你看不到输出
tail --follow=name file.txt | grep something > output.txt
since grep will buffer its own output.
因为 grep 会缓冲它自己的输出。
Use the --line-buffered switch for grep to work around this:
对 grep 使用 --line-buffered 开关来解决这个问题:
tail --follow=name file.txt | grep --line-buffered something > output.txt
This is useful if you want to get the results of the follow into the output.txt file as rapidly as possible.
如果您想尽快将后续结果放入 output.txt 文件中,这将非常有用。
回答by geekosaur
You do know that tail
starts by default with the last ten lines of the file? My guess is everything the cat
version found is well into the past. Try tail -n+1 --follow=name file.txt
to start from the beginning of the file.
您知道tail
默认情况下从文件的最后十行开始吗?我的猜测是cat
发现的版本已经成为过去。尝试tail -n+1 --follow=name file.txt
从文件的开头开始。
回答by Billy Moon
works for me on Mac without --follow=name
在没有 Mac 的情况下对我有用 --follow=name
bash-3.2$ tail delme.txt | grep po
position.bin
position.lrn
bash-3.2$ tail delme.txt | grep po | grep lr
position.lrn
回答by radman
Figured out what was going on here. It turns out that the command is working it's just that the output takes a long time to reach the console (approx 120 seconds in my case). This is because the buffer on the standard out is not written each line but rather each block. So instead of getting every line from the file as it was being written I would get a giant block every 2 minutes or so.
想通了这里发生了什么。事实证明该命令正在运行,只是输出需要很长时间才能到达控制台(在我的情况下大约为 120 秒)。这是因为标准输出上的缓冲区不是写入每一行而是每个块。因此,不是在写入文件时从文件中获取每一行,而是每 2 分钟左右得到一个巨大的块。
It should be noted that this works correctly:
应该注意的是,这可以正常工作:
tail file.txt | grep something | grep something
It is the following of the file with --follow=name
that is problematic.
这--follow=name
是有问题的文件的以下内容。
For my purposes I found a way around it, what I was intending to do was capture the output of the first grep to a file, so the command would be:
为了我的目的,我找到了一种解决方法,我打算做的是将第一个 grep 的输出捕获到一个文件中,因此命令是:
tail --follow=name file.txt | grep something > output.txt
A way around this is to use the script
command like so:
解决此问题的一种方法是使用如下script
命令:
script -c 'tail --follow=name file.txt | grep something' output.txt
Script captures the output of the command and writes it to file, thus avoiding the second pipe.
脚本捕获命令的输出并将其写入文件,从而避免第二个管道。
This has effectively worked around the issue for me, and I have explained why the command wasn't working as I expected, problem solved.
这对我来说有效地解决了这个问题,我已经解释了为什么命令没有按我预期的那样工作,问题解决了。
FYI, These other stackoverflow questions are related:
Trick an application into thinking its stdin is interactive, not a pipe
Force another program's standard output to be unbuffered using Python
仅供参考,这些其他 stackoverflow 问题是相关的:
诱使应用程序认为其标准输入是交互式的,而不是管道
强制另一个程序的标准输出使用 Python 进行无缓冲
回答by Sanjay
grep pattern filename | grep pattern | grep pattern | grep pattern ......
grep 模式文件名 | grep 模式 | grep 模式 | grep 模式......