为什么在使用grep两次时没有显示输出?

时间:2022-11-21 13:55:54

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:

编辑:要回答到目前为止的问题,该文件肯定包含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

5 个解决方案

#1


84  

You might also run into a problem with grep buffering when inside a pipe. ie, you don't see the output from

您还可能在管道中遇到grep缓冲的问题。ie,你看不到输出。

   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工作围绕这个:

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.

如果您想要在输出中获得以下结果,这是非常有用的。txt文件越快越好。

#2


6  

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秒)。这是因为标准输出上的缓冲区不是写每一行,而是写每一个块。所以不是从文件中获取每一行,而是每两分钟得到一个巨大的块。

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 -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 * 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

顺便提一下,这些其他的*问题是相关的:让应用程序认为它的stdin是交互式的,而不是使用Python强制另一个程序的标准输出不被缓存

#3


-1  

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默认从文件的最后10行开始吗?我的猜测是所有猫版本的发现都是过去了。尝试尾部-n+1——follow=name文件。txt从文件的开头开始。

#4


-1  

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

#5


-1  

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

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

#1


84  

You might also run into a problem with grep buffering when inside a pipe. ie, you don't see the output from

您还可能在管道中遇到grep缓冲的问题。ie,你看不到输出。

   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工作围绕这个:

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.

如果您想要在输出中获得以下结果,这是非常有用的。txt文件越快越好。

#2


6  

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秒)。这是因为标准输出上的缓冲区不是写每一行,而是写每一个块。所以不是从文件中获取每一行,而是每两分钟得到一个巨大的块。

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 -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 * 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

顺便提一下,这些其他的*问题是相关的:让应用程序认为它的stdin是交互式的,而不是使用Python强制另一个程序的标准输出不被缓存

#3


-1  

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默认从文件的最后10行开始吗?我的猜测是所有猫版本的发现都是过去了。尝试尾部-n+1——follow=name文件。txt从文件的开头开始。

#4


-1  

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

#5


-1  

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

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