Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash terminal output - highlight lines containing some text

Tags:

bash

shell

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.

like image 331
Cigarillo Avatar asked Mar 12 '15 05:03

Cigarillo


2 Answers

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 --color -e 'FAIL' -e '^' <<<$'Foo\nBar FAIL Baz\nIck'

Grep output with multiple Colors? describes a hack for getting multiple colors if you need that.

like image 115
tripleee Avatar answered Oct 12 '22 20:10

tripleee


If you're happy to install a BASH script and ack, the hhlighter package has useful default colours and an easy interface https://github.com/paoloantinori/hhighlighter:

hhighlight example

You can use it like so to highlight rows that start with FAIL:

h -i 'FAIL.*'

or that contain FAIL:

h -i '.*FAIL.*'

or for various common log entries:

h -i '.*FAIL.*' '.*PASS.*' '.*WARN.*'
like image 43
Andy Avatar answered Oct 12 '22 19:10

Andy