Piping of grep is not working with tail? [duplicate]

笑着哭i 提交于 2020-03-19 07:01:13

问题


I am trying to debug one scenario by checking the logs, here is my command

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

Basically what I am trying to to is that, of all of the line, I am printing lines with enimation in it, then of all of the animation, I want to see the animation with "tap" in it.

Here is the sammple data for which it is returning empty results

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

This is returning empty results.

While if I run this command

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

it returns correct results. Can someone please explain to me, what is the difference between the above two command and why there is a discrepancy in the results. They both seem identical to me.


回答1:


grep is buffering it's output. To tell GNU grep to spit out output line-by-line you need to use --line-buffered option in grep to make it work:

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

As per 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.



回答2:


The output of the grep in the middle is not a terminal, so it is using block buffering instead of line buffering. You have to force line buffering using --line-buffered option.

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

In case of other commands, which don't provide such an option, you can use stdbuf command to force line buffering, e.g.:

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



回答3:


Your output is getting buffered. Try:

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



回答4:


Try adding the -E option to grep. A lot of reflex features don't work without it. I usually refer to it as the "yes, I do know what I'm doing" option.



来源:https://stackoverflow.com/questions/26362450/piping-of-grep-is-not-working-with-tail

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!