Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color specific words in Linux terminal whenever they appear

I have a bunch of scripts running in my terminal (and I don't have the ability to edit them) which output messages to the terminal. I would like my terminal to automatically color specific words in the output.

For example, some of the scripts output FAIL when a test fails. How can I configure the terminal to color this specific word, any time it appears, to be in a specific color (for example, red).

like image 534
Turambar Avatar asked Oct 10 '13 10:10

Turambar


People also ask

How do you color text in Linux terminal?

A script can use escape sequences to produce colored text on the terminal. Colors for text are represented by color codes, including, reset = 0, black = 30, red = 31, green = 32, yellow = 33, blue = 34, magenta = 35, cyan = 36, and white = 37.

Which command can be used to modify the color of the text which appears on screen?

The correct answer is Font color. You can change the colour of the text in your Word document. Select the text that you want to change. On the Home tab, in the Font group, choose the arrow next to Font Color, and then select a colour.

How do you show colors in terminal?

Your problem is that most probably colored output is disabled in your terminal. Then go in your Terminal settings -> Preferences -> Profiles -> Text -> Display ANSI colors. Open a new terminal and you should be ready to go!


2 Answers

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.

like image 137
Andrew Aylett Avatar answered Oct 01 '22 20:10

Andrew Aylett


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.

like image 1
JohnKollar Avatar answered Oct 01 '22 19:10

JohnKollar