Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piping tail output though grep twice

Going with a typical Apache access log, you can run:

tail -f access_log | grep "127.0.0.1" 

Which will only show you the logs (as they are created) for the specified IP address.

But why does this fail when you pipe it though grep a second time, to further limit the results?

For example, a simple exclude for ".css":

tail -f access_log | grep "127.0.0.1" | grep -v ".css" 

won't show any output.

like image 272
Craig Francis Avatar asked Dec 13 '12 11:12

Craig Francis


People also ask

Does grep work with tail?

The tail command is being executed on the output of the grep command and so will print the last 4 lines of the output of grep. If for example there are 50 occurrences of "&" in input. txt, only the last 4 will print.

How do you pipe a grep output?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".

What does the V flag do for grep?

grep -v “pattern” file. The -v flags invert the search results, as it displays those strings in file. txt which do not contain pattern .

Does grep return a string?

The grep command searches a text file based on a series of options and search string and returns the lines of the text file which contain the matching search string.


1 Answers

I believe the problem here is that the first grep is buffering the output which means the second grep won't see it until the buffer is flushed.

Try adding the --line-buffered option on your first grep:

tail -f access_log | grep --line-buffered "127.0.0.1" | grep -v ".css" 

For more info, see "BashFAQ/009 -- What is buffering? Or, why does my command line produce no output: tail -f logfile | grep 'foo bar' | awk ..."

like image 56
Shawn Chin Avatar answered Sep 24 '22 00:09

Shawn Chin