为什么两次使用 grep 没有输出?

发布于 2024-10-26 14:30:25 字数 583 浏览 2 评论 0原文

基本上我想知道为什么这不输出任何内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凉城已无爱 2024-11-02 14:30:25

在管道内时,您还可能会遇到 grep 缓冲问题。
即,您看不到输出,

   tail --follow=name file.txt | grep something > output.txt

因为 grep 将缓冲其自己的输出。

使用 grep 的 --line-buffered 开关来解决这个问题:

tail --follow=name file.txt | grep --line-buffered something > output.txt

如果您想尽快将后续结果放入 output.txt 文件中,这非常有用。

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

since grep will buffer its own output.

Use the --line-buffered switch for grep to work around this:

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.

享受孤独 2024-11-02 14:30:25

弄清楚这里发生了什么。事实证明,该命令正在运行,只是输出需要很长时间才能到达控制台(在我的例子中大约需要 120 秒)。这是因为标准输出上的缓冲区不是每一行写入的,而是每个块写入的。因此,我不是在写入文件时获取文件中的每一行,而是每隔 2 分钟左右得到一个巨大的块。

应该注意的是,这可以正常工作:

tail file.txt | grep something | grep something

有问题的是带有 --follow=name 的文件的以下内容。

出于我的目的,我找到了一种解决方法,我打算做的是将第一个 grep 的输出捕获到文件中,因此命令是:

tail --follow=name file.txt | grep something > output.txt

解决此问题的方法是使用 script命令如下:

script -c 'tail --follow=name file.txt | grep something' output.txt

脚本捕获命令的输出并将其写入文件,从而避免第二个管道。

这有效地解决了我的问题,并且我已经解释了为什么该命令没有按我的预期工作,问题已解决。

仅供参考,这些其他 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:

tail file.txt | grep something | grep something

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:

tail --follow=name file.txt | grep something > output.txt

A way around this is to use the script command like so:

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

初见 2024-11-02 14:30:25

这也是 Fish shell 的一个问题,其中调用“grep”会调用 /usr/share/fish/functions/grep.fish,而第二个 grep 根本没有运行,例如

>> sudo tail -f /var/log/syslog | grep kernel | grep --nonexistentoption

:无输出。
那么解决方法是 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:

>> sudo tail -f /var/log/syslog | grep kernel | grep --nonexistentoption

produces no output.
The fix then, is to sudo rm /usr/share/fish/functions/grep.fish

白昼 2024-11-02 14:30:25

您是否知道 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 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.

偷得浮生 2024-11-02 14:30:25

我在 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

works for me on Mac without --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
乞讨 2024-11-02 14:30:25

grep 模式文件名 | grep 模式 | grep 模式 | grep 模式......

grep pattern filename | grep pattern | grep pattern | grep pattern ......

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文