Color specific words in Linux terminal whenever they appear

To colorize the text output from a command one might try piping the output of the command into sed, such as the following:

yourcommand | sed -e 's/FAIL/^[[01;31mFAIL^[[00m/g' -e 's/SUCCESS/^[[01;32mSUCCESS^[[00m/g'

One could also place those substitution rules into a text file (e.g. colorize.sed) and use the following:

yourcommand | sed -f colorize.sed

This will allow different colors to be assigned to different match strings. Note that in my examples the '^[' means the escape character, not a carat followed by a square bracket. The escape character can be entered into the rule by typing Ctrl-V and then pressing the escape key.

The colors/effects that are available for these tty codes are as follows:

Foreground colors: Black=30, Blue=34, Cyan=36, Green=32, Purple=35, Red=31, White=37, Yellow=33

Background colors: Black=40, Blue=44, Cyan=46, Green=42, Purple=45, Red=41, White=47, Yellow=43

Effects: Normal=00, Bold=01, Dim=02, Underlined=04, Blinking=05, Reversed=07, Hidden=08

These can also be combined with a semicolon as I did (i.e. 01;31 to get bold red).

Note that the '^[00m' code is required to disable the previous color/effect, otherwise the color/effect will persist after the match string. Also note that some of the effects don't work (or work as I described) with some terminal emulators.

I hope that I'm not just repeating what someone else has already said, because I didn't read through the entire discussion thread.


It's probably easier to colour the words yourself, rather than getting the terminal to colour them for you. If you can't edit the scripts that create the output, can you filter them through something else?

At the most likely to be available end of the scale you could pipe your output through grep:

tail -F logfile | grep --color -P "FAIL|"

This matches either "FAIL" or "", and highlights the matched portion of the string.

You could further use something more specialised, as described in this blog post, for example.