supercat seems to do what you're looking for.
Description-en: program that colorizes text for terminals and HTML
Supercat is a program that colorizes text based on matching regular
expressions/strings/characters. Supercat supports html output as well
as standard ASCII text. Unlike some text-colorizing programs that
exist, Supercat does not require you to have to be a programmer to
make colorization rules.
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.
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:
grep -E --color "\b(PASS|FAIL)\b|$"
This example uses
-E extended regexp option, but
-G basic regexp,
-F fixed-string, and
-P PCRE can also be made to 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).
\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 :)
# Requires: tempfile from debian-utils, getopt from util-linux, and supercat
CFGFILE=$(tempfile -p spc)
Highlight regexp patterns found on stdin or files specified on command
line with specified colours.
Usage: $SCRIPTNAME [ --colour "pattern" ...] [FILE]
run-script.sh | $SCRIPTNAME --green PASS --red FAIL
# Format definition from the spc man page:
#HTML Color Name Col A N T RE / String / Characters
FMT="%-20s %3s %1s %1s %1s (%s)\n"
printf "$FMT" "$COLOR" "$COLOR" - 0 r "$PATTERN" >> "$CFGFILE"
# uses the "getopt" program from util-linux, which supports long
# options. The "getopts" built-in to bash does not.
-o 'hk:r:g:y:b:m:c:w:' \
-l 'help,black:,red:,green:,yellow:,blue:,magenta:,cyan:,white:' \
-n "$0" -- "$@")
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"
while true ; do
case "$1" in
-k|--bla*) add_color_to_config blk "$2" ; shift 2 ;;
-r|--red) add_color_to_config red "$2" ; shift 2 ;;
-g|--gre*) add_color_to_config grn "$2" ; shift 2 ;;
-y|--yel*) add_color_to_config yel "$2" ; shift 2 ;;
-b|--blu*) add_color_to_config blu "$2" ; shift 2 ;;
-m|--mag*) add_color_to_config mag "$2" ; shift 2 ;;
-c|--cya*) add_color_to_config cya "$2" ; shift 2 ;;
-w|--whi*) add_color_to_config whi "$2" ; shift 2 ;;
-h|--hel*) usage ; exit 0 ;;
--) shift ; break ;;
*) echo 'Unknown option!' ; exit 1 ;;
spc -R -c "$CFGFILE" "$@"
rm -f "$CFGFILE"