grep的pipe道不能用尾巴工作?

我试图通过检查日志来debugging一个场景,这里是我的命令

tail -f eclipse.log | grep 'enimation' | grep -i 'tap' 

基本上我想要的是,在所有的行中,我在其中打印带有animation的线条,然后是所有的animation,我想看“animation”中的animation。

以下是返回空结果的样例数据

 *******enimation error*********TapExpand *******enimation error*********TapShrink 

这是返回空的结果。

而如果我运行这个命令

  tail -f eclipse.log | grep -i 'enimation.*tap' 

它会返回正确的结果。 有人可以向我解释一下,上述两个命令有什么区别,为什么结果有差异。 他们都和我一样。

grep缓冲它的输出。 为了让GNU grep能够逐行扫描输出,需要在grep使用--line-buffered选项来使其工作:

 tail -f eclipse.log | grep --line-buffered 'enimation' | grep --line-buffered -i 'tap' 

按照man grep

 --line-buffered Force output to be line buffered. By default, output is line buffered when standard output is a terminal and block buffered otherwise. 

中间grep的输出不是终端,所以使用了块缓存而不是行缓存。 您必须使用--line-buffered选项强制行缓冲。

 tail -f eclipse.log | grep --line-buffered 'enimation' | grep -i 'tap' 

如果其他命令不提供这样的选项,则可以使用stdbuf命令强制行缓冲,例如:

 tail -f eclipse.log | stdbuf -o1 grep 'enimation' | grep -i 'tap' 

您的输出正在缓冲。 尝试:

 tail -f eclipse.log | grep --line-buffered 'enimation' | grep --line-buffered -i 'tap' 

尝试将-E选项添加到grep 。 没有它,许多反射功能就无法工作。 我通常把它称为“是的,我知道我在做什么”选项。