24

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).

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Turambar
  • 293
  • 1
  • 3
  • 8
  • Duplicate: http://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal – Jongware Oct 10 '13 at 10:10
  • 4
    @Jongware: not a duplicate; this question is about making the terminal look for the words and color them, not about having a program do colored output. (It's off-topic for SO, though). – Wooble Oct 10 '13 at 10:14
  • 2
    Why was this downvoted? It's a good question, though I don't know whether it's on-topic for SO; perhaps it should be migrated to SuperUser? – Mark Amery Oct 10 '13 at 10:18
  • I suspect this depends on the terminal emulator. For the general case, a `screen`-like tool could be made to do this. – Fred Foo Oct 10 '13 at 10:35
  • I read the "Duplicate" before posting but wasn't what i am asking about, Jongware. – Turambar Oct 10 '13 at 11:42

3 Answers3

18

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.

Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
  • You mean, logging the output and then running grep on it? Actually using {LINE OF SCRIPT} | grep --color "FAIL" seems to give me the output where FAIL is, but not excactly what I needed. – Turambar Oct 10 '13 at 11:10
  • 1
    That should be `grep --color -P "FAIL|"` which is slightly different from what you wrote in your comment. – Andrew Aylett Oct 10 '13 at 13:41
  • yeah.. in order for all the output to be shown in the end, with the colored words, there must be an |. I used it a bit creatively, in order to keep only the lines of the output I needed. Thanx! – Turambar Oct 10 '13 at 13:59
  • there are persons that need to be checking logcats for a long time until something happens, I think the OP is trying to solve that kind of problem. – htellez Oct 10 '15 at 04:37
  • 2
    `-P` is not a valid option on Mac. I used: `... | egrep --color "FAIL| "`, because all my lines do contain at least one space character, and coloring those is not visible. `egrep "FAIL|"` doesn't work: `egrep: empty (sub)expression`. Or you can install GNU' grep; see http://stackoverflow.com/a/22704387/432306 – Guillaume Boudreau Feb 01 '17 at 15:23
1

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.

JohnKollar
  • 11
  • 1
0

If the grep command installed in your linux/unix flavour doesn't has the -P parameter you can use:

egrep --color "\b(place_here_what_you_are_looking_for)\b|$"
NetVicious
  • 3,848
  • 1
  • 33
  • 47