Grep piping not working with tail?

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

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

      

Basically what I am trying to do is that from the whole line I print lines with enimation in it and then from all animation I want to see animation with "tap" in it.

Here is the sammple data for which it returns empty results

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

      

This returns empty results.

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 command and why there is a discrepancy in the results. They both seem identical to me.

+3


source to share


4 answers


grep

performs output buffering. To tell GNU grep to print output line by line , you need to use the option --line-buffered

in grep

to get it to work:

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

      



By 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.

      

+5


source


The output grep

in the middle is not a terminal, so it uses block buffering instead of line buffering. You have to force the line buffering with a parameter --line-buffered

.

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

      



In case of other commands that do not provide such an option, you can use the command stdbuf

to force line buffering, for example:

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

      

+3


source


You get buffering. Try:

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

      

+1


source


Try adding a parameter -E

to grep

. A lot of reflex functions don't work without it. I usually call this the "yes, I do know what I'm doing" option .

-2


source







All Articles