8

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.

Community
  • 1
  • 1
Cigarillo
  • 327
  • 4
  • 9
  • Depending on your needs, coloring the standard error stream might actually be preferable to looking for keywords. I'm a big fan of [stderred](https://github.com/sickill/stderred) for this. – Kyle Strand Mar 12 '15 at 07:23

5 Answers5

11

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.

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • ++, but as an aside: even though it _should_ work, your command _crashes_ the BSD `grep` v2.5.1 on macOS 10.12.4 (it works fine with the same `grep` version on PC-BSD 10.1, for instance). – mklement0 May 06 '17 at 20:44
  • 1
    @mklement0 That's curious! It only seems to crash after completing, but still of course not a Good Thing. – tripleee Jun 14 '19 at 07:53
  • 1
    `grep -e '.*' -e '^'` also crashes, but the coloring looks like it somehow gets stuck in the second `-e` after the first line. Removing the `--color` option avoids the crash. – tripleee Jun 14 '19 at 07:55
3

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.*'
Andy
  • 17,423
  • 9
  • 52
  • 69
  • 1
    First helpful answer to this topic (even to similar stackoverflow questions)! Possibility to highlight different words in different colors. Works on macos with iTerm2 and zsh – Business Tomcat Apr 18 '18 at 10:29
2

Building on tripleee's answer, following command will highlight the matching line red and preserve the other lines:

 your_command | grep --color -e ".*FAIL.*" -e "^"

If you prefer a completely inverted line, with gnu grep:

 your_command | GREP_COLORS='mt=7' grep --color -e ".*FAIL.*" -e "^"

(updated with mklement0 feedback)

Cruiser
  • 121
  • 4
  • 2
    The first command _duplicates_ tripleee's answer (except for using a pipeline instead of a here-string). The 2nd command is helpful, except that you should mention that it requires _GNU_ `grep` (won't work on BSD/macOS, for instance) and perhaps make it clearer that the _entire line_ is inverted. – mklement0 May 06 '17 at 20:50
2

This will highlight not only a word, but the whole line:

echo "foo bar error baz" | egrep --color '*.FAIL.*|$'

A search phrase should be enclosed by .* on either side. It will cause highlighting before and after the search word or phrase.

References

  1. https://unix.stackexchange.com/a/330613/341457
  2. How .* (dot star) works?
Jonathan Holvey
  • 697
  • 9
  • 27
Gryu
  • 2,102
  • 2
  • 16
  • 29
0

add into your .profile or .bashrc following lines:

hilite() {
    REGEX_SED=$(echo $1 | sed "s/[|()]/\\\&/g");
    sed "s/$REGEX_SED/\x1b[7m&\x1b[0m/g"
}

then * you can enter something like this:

cat example.txt | hilite FAIL

or

grep FAIL example.txt | hilite FAILURE
  • once you have to execute a "source .profile" or ". .bashrc" or a second login, to make it effective
cjonas
  • 71
  • 1
  • 4