‘tail -f’ with highlight

This is an example of how you can use tail with colors.

# tail -f /var/log/maillog | perl -pe 's/colorthisword/\e[1;32;40m$&\e[0m/g'

The ;;; values explained

0 all attributes off
1 bold
32 foreground green
40 background black

“colorthisword” 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

Colors
This vector holds the colors used for SGR control sequences parameters 30 to 37 \(foreground colors) and 40 to 47 (background colors).

Parameter Color
30 40 black
31 41 red
32 42 green
33 43 yellow
34 44 blue
35 45 magenta
36 46 cyan
37 47 white

Note
This only works on ANSI terminals

One thought on “‘tail -f’ with highlight

  1. Hello and thank you. This article really helped me.
    It is all ok, but in examples \b((foo|bar)\b and .*\b((foo|bar)\b.* you have two open prackets and there should be only one (\b(foo|bar)\b and .*\b(foo|bar)\b.*)

Comments are closed.