When I get output in bash I get my standard 2 colour screen. Is there any way I can, by default, highlight a line if it contains some key text output?
E.g. if it contains the word "FAIL" then the line is coloured red.
I’ve read this https://unix.stackexchange.com/questions/46562/how-do-you-colorize-only-some-keywords-for-a-bash-script but am looking for something simpler than having to write a wrapper script which I’d inevitably have to debug at some time in the future.
Building on tripleee's answer, following command will highlight the matching line red and preserve the other lines:
If you prefer an inverted line:
If you're happy to install a BASH script and
ack
, thehhlighter
package has useful default colours and an easy interface https://github.com/paoloantinori/hhighlighter:You can use it like so to highlight rows that start with
FAIL
:or that contain
FAIL
:or for various common log entries:
For a simple workaround, pipe it through
grep --color
to turn some words red.Add a fallback like
^
to print lines which do not contain any matches otherwise.Grep output with multiple Colors? describes a hack for getting multiple colors if you need that.