Tail and Grep: Display All Output but Highlight Search Matches

  • Home /
  • Blog Posts /
  • Tail and Grep: display all output but highlight search matches

I tail logs quite a lot.

An example would be to tail the Apache2 access logs.

This is simple to do:

tail -f /var/log/apache2/access.log

This will show a trail of all access log entries as they come in.

What if we only wanted to see when Googlebot accessed the site?

Highlight entries

We could filter only these results:

tail -f /var/log/apache2/access.log | grep -i googlebot

This will tail all logs, and grep will only shows results for case-insensitive instances of googlebot entries.

But what about if we want to show all the other logs at the same time, but with googlebot being highlighted?

Highlight entries and keep everything else

This can be done as follows:

tail -f access.log | grep --color -E -i "googlebot|$"

What this does is tail all access log entries, then highlight all case-insensitive results of googlebot, which will be coloured/lighlighted, and also show the remainer of the entries too.

In short, the above will show all entries as they come into the log, but only highlight the string googlebot (case-insensitive).