Skip to content

How to highlight keywords when using tail

by Andrew McCombe on October 16th, 2012

How to highlight keywords when using tail

Have you ever been tailing a log file looking for a specific keyword or phrase? Maybe you’re tailing an Exim log watching for an email, or an Apache log file for a specific request. Wouldn’t it be really great if you could highlight the keyword or phrase? Here’s How to highlight keywords when using tail.

tail -f file.log | perl -pe 's/keyword/\e[1;31;43m$&\e[0m/g'

The “\e…/g” section can be replaced by some semicolon-separated integers, with the meaning:

0 : all attributes off
1 : bold
31 : foreground red
43 : background yellow

“keyword”, of course, can be any perl regular expression:

(foo|bar) highlight the strings foo and bar
\b((foo|bar)\b highlight the words foo and bar
.*\b((foo|bar)\b.* highlight the whole line that contains the words foo or bar

One Comment
  1. Hi,
    I am finding a wordpress plugin which helps me to convert a keyword to a image. For example: i have keyword “Rapidshare” in many posts, i want “Rapidshare” keyword will display as Rapidshare logo image. Please help me to do…

    Thanks so much !

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS

euperia.com / Website Engineer located at , Midlands, UK . Reviewed by Andrew McCombe rated: 1 / 5