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.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With