Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

linux - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...