Tail and Grep: display all output but highlight search matches

0 min read 174 words

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).

Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags