I am running some unit test code. The unit test code outputs regular text. There is a lot of the text so I want to highlight for the user important keywords.
In this case the keywords are "PASS" and "FAIL".
How do you colorize "PASS" to be green and "FAIL" to be red?
Best Answer
supercat
seems to do what you're looking for.There doesn't seem to be any way to tell it what to colourise on the command line, you have to specify a config file.
I seem to recall there used to be a program called 'hilite' or 'hl' that highlighted text that matched a pattern (like
grep --colour
, but displaying non-matching lines too), but I couldn't find it when I searched for it.Finally, GNU
grep
can be used to highlight patterns - but only one colour can be used (i.e. you can't have PASS in green and FAIL in red, both would be highlighted with the same colour).Pipe your data through something like this:
This example uses egrep (aka
grep -E
), but-G
basic regexp,-F
fixed-string, and-P
PCRE also work.All matches will be highlighted. Default is red, or set the GREP_COLOR env var.
The key to this working is that the final
|$
in the pattern matches end-of-line (i.e. all lines match) so all lines will be displayed (but not colorised).The
\b
are word-boundary markers so that it matches e.g. FAIL but not FAILURE. they're not necessary, so remove them if you want to match partial words.Here's the example wrapper script for supercat that I wrote yesterday. It works, but in writing it, I discovered that supercat doesn't have any option for case-insensitive searches. IMO, that makes the program significantly less useful. It did, however, greatly simplify the script because I didn't have to write a '-i' option :)