Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 657
radman Avatar asked Mar 25 '11 01:03

radman


People also ask

How do you grep twice?

Replace df -h with the command you want to use, and replace head -1 and grep '/$' with the two commands you want to apply to it. The output of both will be displayed in your terminal, though it may be that the output of the former command is displayed after the latter.

What does grep return if it finds nothing?

Indeed, grep returns 0 if it matches, and non-zero if it does not. Hence my comment. In the shell 0 means success.

How do I get grep output?

To Show Lines That Exactly Match a Search String The grep command prints entire lines when it finds a match in a file. To print only those lines that completely match the search string, add the -x option. The output shows only the lines with the exact match.

How can you get something with grep and do not have all output on the screen?

The quiet option ( -q ), causes grep to run silently and not generate any output.


1 Answers

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.

like image 181
simonc Avatar answered Sep 18 '22 00:09

simonc