0

How can I highlight the grep results in different colors (for example, green or red) depending on what's matched?

Currently, I've done this:

grep -H -i "^mail.hostname" WEB-INF/config/mail.props | grep --color=auto -P "\=.*"

which outputs (for example):

WEB-INF/config/mail.props:mail.hostname=localhost

where =localhost is highlighted. (NOTE -- I would like the = sign NOT to be highlighted, but that's another story.)

How can I get the highlight to be in green when it's localhost and in red for any other values (such as, for example, smtpserver)?

UPDATE -- Answer to first 2 comments:

If I do:

grep -H -i "^mail.hostname" WEB-INF/config/mail.props \
    | GREP_COLOR='01;31' grep --color=auto -P '=\K.*' \
    | GREP_COLOR='01;32' grep --color=auto -P '=\Klocalhost'

the line will only be printed (with highlight in green) when it matches localhost... Not the expected result...

user3341592
  • 1,419
  • 1
  • 17
  • 36
  • https://stackoverflow.com/questions/17236005/grep-output-with-multiple-colors and https://unix.stackexchange.com/questions/104350/multicolored-grep might help – Sundeep Jul 18 '18 at 09:44
  • to avoid `=` being highlighted, you can use `grep --color=auto -P '=\K.*'` (note: `=` doesn't need escaping) – Sundeep Jul 18 '18 at 09:46
  • Thanks for the comments… I'm approaching, but not yet there... – user3341592 Jul 18 '18 at 10:03
  • try with `grep --color=always` for the middle grep – Sundeep Jul 18 '18 at 10:20
  • No output anymore with `grep -H -i "^mail.hostname" WEB-INF/config/mail.props | GREP_COLOR='01;31' grep --color=always -P '=\K.*' | GREP_COLOR='01;32' grep --color=auto -P '=\Klocalhost'` – user3341592 Jul 18 '18 at 10:50
  • The 2nd and 3rd greps only output matching lines, so you would only have "localhost" entries. Instead you could match the desired string OR start of line - that will match all lines, so all will be output. Try: `grep -H -i "^mail.hostname" WEB-INF/config/mail.props | GREP_COLOR='01;31' grep -E --color=always '=.*|^' | GREP_COLOR='01;32' grep -E --color=always '=localhost|^'` This includes the "=" in colour, but the `-E` clashes with the `-P` so I had to drop it. – simon3270 Jul 18 '18 at 13:14
  • In fact, the middle grep will catch all lines, since they all match "=.*", so we can drop the `-E` and put the `-P` and `\K` back (so that the "=" isn't coloured). For the 3rd grep, we could just search for "localhost" without the "=", as long as it doesn't match anywhere else in the file, and also grep for "start of line" so that we transfer all of the input lines to the output. So we end up with `| GREP_COLOR='01;31' grep --color=always -P '=\K.*' | GREP_COLOR='01;32' grep -E --color=always 'localhost|^'` – simon3270 Jul 18 '18 at 13:22
  • I was able to modify your ante-penultiame solution, to this: `grep -H -i "^mail.hostname" WEB-INF/config/mail.props | GREP_COLOR='0;30;41' grep -E --color=always '[^=]*$|^' | GREP_COLOR='0;30;42' grep -E --color=always 'localhost|^'` which works OK. Thanks! – user3341592 Jul 18 '18 at 13:30
  • Please publish the best solution to get the credit. – user3341592 Jul 18 '18 at 13:30
  • Still a bit of magic to me, the fact that you grep for the whole line, and that the whole line does not get highlighted! – user3341592 Jul 18 '18 at 13:32

1 Answers1

0

The middle grep here it will find all "=xxxx" and colour the xxxx part in red.

The last grep looks for "localhost" or start of line, and marks them in green. Because "start of line" is not a displayable character, it is not coloured, but because all lines have a "start of line", all of the ones from the middle grep are passed through to the output.

grep -H -i "^mail.hostname" WEB-INF/config/mail.props | GREP_COLOR='01;31' grep --color=always -P '=\K.*' | GREP_COLOR='01;32' grep -E --color=always 'localhost|^'

Thanks to Sundeep for the "grep -P" hint.

And I like the OP's [^=]*$ to match everything after the "=" sign!

simon3270
  • 722
  • 1
  • 5
  • 8
  • What is better to match everthing after the "=" sign? Your regexp with `\K` or mine with `[^=]`? – user3341592 Jul 18 '18 at 13:55
  • Bit of a judgement call - the `-P` one uses Perl regexes, which some of us aren't used to, while yours is a little less obvious (it's the sort of thing I would add a comment to in a shell script). Either is fine! – simon3270 Jul 18 '18 at 14:31
  • For me, the Perl regexes are really a mystery… So, I do prefer the other… Question of habit… Thanks for the explanation. – user3341592 Jul 19 '18 at 07:26
  • Also [^=]*$ will work differently if two '='s are in the line! – anthony Mar 07 '19 at 01:07
  • Simplier (and more common) example... df | grep --color=always -P '% \K.*' – anthony Mar 07 '19 at 01:08