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.
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.
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 " | ".
grep -v “pattern” file. The -v flags invert the search results, as it displays those strings in file. txt which do not contain pattern .
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.
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 ...
"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With