How to highlight keywords when using tail
October 16, 2012
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