Search code examples
linuxfilecommand-linegreppipe

Why no output is shown when using grep twice?


Basically I'm wondering why this doesn't output anything:

tail --follow=name file.txt | grep something | grep something_else 

You can assume that it should produce output I have run another line to confirm

cat file.txt | grep something | grep something_else

It seems like you can't pipe the output of tail more than once!? Anyone know what the deal is and is there a solution?

EDIT: To answer the questions so far, the file definitely has contents that should be displayed by the grep. As evidence if the grep is done like so:

tail --follow=name file.txt | grep something

Output shows up correctly, but if this is used instead:

tail --follow=name file.txt | grep something | grep something

No output is shown.

If at all helpful I am running ubuntu 10.04


Solution

  • You might also run into a problem with grep buffering when inside a pipe. ie, you don't see the output from

       tail --follow=name file.txt | grep something > output.txt
    

    since grep will buffer its own output.

    Use the --line-buffered switch for grep to work around this:

    tail --follow=name file.txt | grep --line-buffered something > output.txt
    

    This is useful if you want to get the results of the follow into the output.txt file as rapidly as possible.