4

I want to search all files in a directory for a Keyword and color it, like:

grep -n "keyword1" * --color

Now I want to highlight all occurrences of keyword2 and keyword3 in an other color.

I managed

grep -n "keyword1 * |egrep --color "keyword2|keyword3|$'

this will color both last keywords but in the same color. but I fail to set several different colors to work with a coloring from the first grep-statement. I don't know how to set the constant GREP_COLORS correctly

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
meister_reineke
  • 364
  • 1
  • 14

1 Answers1

3

Something like this will do the job:

grep --color=always -n "keyword1" *  | GREP_COLOR='1;30;40'  grep --color keyword2
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38
  • I needed to understand all options, so that's what I figured it out by now: - --color=always keeps the word highlighted even through piped greps, so if you're looking for 3 or more words you need to set =always to all commands but the last one - GREP_COLOR='1;30' means thickness 1 (bold) and color 30 (black, bad choice if you use a dark background) I know GREP_COLOR can accept even more complex parameters but I just cannot find an exhaustive documentation (+1 to this answer for the good solution btw) – afe Dec 20 '17 at 09:40
  • It's well documented in `GREP_COLORS` section in `grep man page`. According to the manpage `GREP_COLOR` is `deprecated in favor of GREP_COLORS`. – Arkadiusz Drabczyk Dec 20 '17 at 11:40