Web page archived as of 2018-05-22. Some links & features might not work.

Color my logs

I was looking for a simple way to colorize text such as output of a tail from within my bash shell. I wanted something simple as KISS, of course.

lwatch seems to be tailored for logfiles. ccze is a bit of overkill. MultiTail while sure a nice tool just did not behave as expected, and one has to define color schemes in separate files.

Luckily, I remembered that it is pretty easy to colorize the shell's prompt or any echo lines. So, I quickly checked if a sed command like

sed -e "s/\(my regex\)/$color\1$resetcolor/g"

actually works if colors are stored in environment variables. And it does :-)

Thus, I wrote a small script mycolorize that allows me to do something like

tail -f procmail.log | mycolorize red '^From: .*' blue '^ Subject: .*'

For details see A simple colorize for the shell

Discussion

Andreas Schamanek, 2008-04-18 21:35, 2008-04-20 23:05

You can use the script to beep when your system load reaches some limit with something like

while true ; do cat /proc/loadavg ; sleep 3 ; done | mycolorize red '^[1-9]\+\.[0-9]\+' beep

This would beep when the load is 1 or higher. It's based on the content of /proc/loadavg which on Debian systems is something like 1.00 0.88 0.48 3/107 28593.

The following would beep every 5 seconds at a load >= 2, and it highlights also the average values for the past 5 and 15 minutes.

while true ; do cat /proc/loadavg ; sleep 5 ; done \
| mycolorize red '^[2-9]\+\.[0-9]\+' beep \
    pink '^[^ ]\+ \/[1-9]\+\.[0-9]\+' beep \
    blue '^[^ ]\+ [^ ]\+ \/[1-9]\+\.[0-9]\+' beep

You can see I am no friend of blue ;-)

Daniel Mack, 2009-03-13 17:44

Very cool, thanks for sharing this. Just what I was looking for :)

Andreas Schamanek, 2012-10-12 00:25

Recently, I converted the sample script to use sed -r, and today I fixed a bug with (…) groups when used in front of \/. The updated script is available at the end of A simple colorize for the shell. Or download it directly: mycolorize-r.sh

Andreas Schamanek, 2013-07-02 21:22

Colout by nojhan is a similar tool to "add colors to a text stream in your terminal". It's written in Python and comes with a bunch of great features such as "256 colors mode, colormaps, themes and source code syntax coloring" (quoting from the homepage) while still keeping the whole thing simple to use.

Andreas Schamanek, 2014-11-06 17:37

regex-markup is another program that "performs regular expression-based text markup according to user-defined rules". Rules are stored in separate files. The program is written in C. It supports hierarchical rules.

Andreas Schamanek, 2015-05-13 17:27

Supercat "colorizes text based on matching regular expressions/strings/characters. Supercat supports html output as well as standard ASCII text."

Andreas Schamanek, 2017-01-26 17:52

nachoparker wrote a simple wrapper around mycolorize that uses a preset list of colors: Colorize your stdout with xcol (p. 2017-01-23, a. 2017-01-26, archived 2022-03-31)

 
blog/080413_color_my_logs.txt · Last modified: 2013-06-03 23:36 by andreas