参见英文答案 > How to ‘grep’ a continuous stream? 11个
我试图通过检查日志来调试一个场景,这是我的命令
tail -f eclipse.log | grep 'enimation' | grep -i 'tap'
基本上我想要的是,在所有的线条中,我在其中打印带有enimation的线条,然后在所有动画中,我想看到带有“tap”的动画.
以下是返回空结果的sammple数据
*******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'
按照男人的说法:
--line-buffered
Force output to be line buffered. By default, output is line buffered when standard
output is a terminal and block buffered otherwise.