为什么两次使用 grep 没有输出?
基本上我想知道为什么这不输出任何内容:
tail --follow=name file.txt | grep something | grep something_else
您可以假设它应该产生输出我已经运行另一行来确认
cat file.txt | grep something | grep something_else
似乎您不能多次通过管道传输 tail 的输出!?有人知道这是什么交易吗?有解决办法吗?
编辑: 为了回答到目前为止的问题,该文件肯定包含 grep 应该显示的内容。作为 grep 是否按如下方式完成的证据:
tail --follow=name file.txt | grep something
输出正确显示,但如果改为使用:
tail --follow=name file.txt | grep something | grep something
则不显示输出。
如果有帮助的话我正在运行 ubuntu 10.04
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?
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在管道内时,您还可能会遇到 grep 缓冲问题。
即,您看不到输出,
因为 grep 将缓冲其自己的输出。
使用 grep 的 --line-buffered 开关来解决这个问题:
如果您想尽快将后续结果放入 output.txt 文件中,这非常有用。
You might also run into a problem with grep buffering when inside a pipe.
ie, you don't see the output from
since grep will buffer its own output.
Use the --line-buffered switch for grep to work around this:
This is useful if you want to get the results of the follow into the output.txt file as rapidly as possible.
弄清楚这里发生了什么。事实证明,该命令正在运行,只是输出需要很长时间才能到达控制台(在我的例子中大约需要 120 秒)。这是因为标准输出上的缓冲区不是每一行写入的,而是每个块写入的。因此,我不是在写入文件时获取文件中的每一行,而是每隔 2 分钟左右得到一个巨大的块。
应该注意的是,这可以正常工作:
有问题的是带有
--follow=name
的文件的以下内容。出于我的目的,我找到了一种解决方法,我打算做的是将第一个 grep 的输出捕获到文件中,因此命令是:
解决此问题的方法是使用
script
命令如下:脚本捕获命令的输出并将其写入文件,从而避免第二个管道。
这有效地解决了我的问题,并且我已经解释了为什么该命令没有按我的预期工作,问题已解决。
仅供参考,这些其他 stackoverflow 问题是相关的:
欺骗应用程序认为其标准输入是交互式,而不是管道
使用 Python 强制另一个程序的标准输出不缓冲
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.
It should be noted that this works correctly:
It is the following of the file with
--follow=name
that is problematic.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:
A way around this is to use the
script
command like so: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
这也是 Fish shell 的一个问题,其中调用“grep”会调用 /usr/share/fish/functions/grep.fish,而第二个 grep 根本没有运行,例如
:无输出。
那么解决方法是
sudo rm /usr/share/fish/functions/grep.fish
This is also a problem with fish shell, where invoking "grep" invokes
/usr/share/fish/functions/grep.fish
, and the second grep is not being run at all, for example:produces no output.
The fix then, is to
sudo rm /usr/share/fish/functions/grep.fish
您是否知道
tail
默认从文件的最后十行开始?我的猜测是cat
版本发现的一切都已经成为过去。尝试tail -n+1 --follow=name file.txt
从文件开头开始。You do know that
tail
starts by default with the last ten lines of the file? My guess is everything thecat
version found is well into the past. Trytail -n+1 --follow=name file.txt
to start from the beginning of the file.我在 Mac 上工作,无需
--follow=name
works for me on Mac without
--follow=name
grep 模式文件名 | grep 模式 | grep 模式 | grep 模式......
grep pattern filename | grep pattern | grep pattern | grep pattern ......