Subject: Re: bug#22656: How to grep two patterns in a line with correct coloring?

From: Zev Weiss <[email-masked]>
Date: Mon, 11 Apr 2016 00:39:00 -0500

On Sun, Apr 10, 2016 at 09:45:49PM -0700, Paul Eggert wrote:
>This works for me:
>
>grep --color=always -E 'word1|word2'
>

Isn't that a different search?  The original question asked about 
colorizing both patterns in the output of

  grep word1 | grep word2

(i.e. lines that contain *both* word1 and word2), whereas egrepping for 
'word1|word2' outputs lines that contain *either*.

Gary Johnson suggested

  grep --color=always word1 | grep word2

which seems like it should work in most cases, but would break down in 
cases where word2 overlaps a boundary of word1 (since the escape codes 
for colorizing the word1 match would throw off the matching of word2).  
So in full generality it seems like you'd have to do something like:

  grep word1 | grep word2 | grep -E 'word1|word2'

(so that the first two just filter and only the final one colorizes).

Granted,

  echo abc | grep -E --color 'ab|bc'

only colorizes the "ab" part of the output anyway (I'm assuming that's 
expected, and not itself a bug, but I'm not certain).

Zev