亲宝软件园·资讯

展开

关于Grep的多次管道过滤的问题及解决

技术小黑屋 人气:0

Grep的多次管道过滤问题

在日常的开发过程中,我们利用grep可以方便快捷的查找感兴趣的日志内容,极大地提升了开发和排错效率。但是有时候,我们也会遇到一些问题,比如。

tail -f crazy.log | grep Hello
Hello,printting from Ruby
Hello,Time is 1566096393
Hello,printting from Ruby
Hello,Time is 1566096393
Hello,printting from Ruby
Hello,Time is 1566096393
Hello,printting from Ruby
Hello,Time is 1566096393
Hello,printting from Ruby
Hello,Time is 1566096393

那么当我们再次增加一个过滤是,却没有内容(立即)产生了

➜ /tmp tail -f crazy.log | grep Hello | grep Time

如何解决

tail -f crazy.log | grep --line-buffered Hello | grep Time
Hello,Time is 1566096393
Hello,Time is 1566096393
Hello,Time is 1566096393
Hello,Time is 1566096393
Hello,Time is 1566096393

如上,我们使用grep的选项--line-buffered即可。

line-buffered 是什么

--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过滤没有内容,因为没有达到块缓冲限制。

以上。

总结

这些仅为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论