If your application is logging events to a file, chances are that you often find yourself using tail to monitor that file. If you are using a logging library, chances are that your logging events are classified in differing orders of severity. Wouldn’t it be nice to make severe errors readily apparent? You can, by using sed.
If you are using a colour-aware terminal, you can use control codes to change the text colour. By inserting these codes with a sed script, tail output can be colour-coded.
For example, supposing you wanted all lines labelled as containing severe errors to be displayed in bright red. This can be accomplished using the following line:
> tail -f mylog.log | sed -e "s/\(.*SEVERE.*\)/^[[1;31m\1^[[0m/g"
^[ represents the ESCAPE character, which can be typed in by pressing
CTRL-V followed by
I wrote this brief tutorial because I could not find instructions specific to this topic by searching Google. No doubt a better method exists to accomplish this colour-coding, but I’m not that bright.