Combine tail -f with grep?
52,298
You almost wrote the answer, which is :
tail -f file.log | grep "foobar"
That's it.
Related videos on Youtube
Author by
user893730
Updated on September 18, 2022Comments
-
user893730 almost 2 years
Hi I wanna keep looking at a log file, but I also don't wanna see irrelevant stuff, I'm only interested in anything with "foobar" in it.
So if I was tailing the file I would do
tail file | grep "foobar"
Now that I'm adding the -f option, is there a way to somehow only show the stuff that I want? using grep or other technique?
-
uSlackr over 12 yearsIt appears your answer is already written here: stackoverflow.com/questions/5427483/…
-
JdeBP over 12 yearsIt appears that xyr answer is in the question.
-
-
user893730 over 12 yearsWoah, you are right, I think I didn't expect this to work, I still don't, isn't piping supposed to happen when a command has finished execution? I guess this shows that it's not, it happens every time there an output, right?
-
sparrow over 12 yearsNo it will launch the two programs in parallel, and the second one (grep) will exit as soon as tail's STDOUT closes. That's the whole point of pipes, data streaming :)
-
ghoti about 11 yearsWhen
grep
is the last line in the pipe, its output is line buffered, so you see the filtered output oftail -f
live, rather than delayed. Note that if you were to use multiplegrep
commands, any whose output was piped would need a--line-buffered
option (assuming GNU or BSD grep) in order to keep this behaviour.