Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 264
Dude Avatar asked Jan 10 '23 15:01

Dude


2 Answers

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.
like image 183
anubhava Avatar answered Jan 12 '23 05:01

anubhava


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'
like image 42
danadam Avatar answered Jan 12 '23 04:01

danadam