GNU bug report logs - #22656
How to grep two patterns in a line with correct coloring?

Previous Next

Package: grep;

Reported by: Peng Yu <pengyu.ut <at> gmail.com>

Date: Sun, 14 Feb 2016 00:11:02 UTC

Severity: normal

Done: Paul Eggert <eggert <at> cs.ucla.edu>

Bug is archived. No further changes may be made.

Full log


Message #16 received at 22656 <at> debbugs.gnu.org (full text, mbox):

From: Zev Weiss <zev <at> bewilderbeest.net>
To: 22656 <at> debbugs.gnu.org, eggert <at> cs.ucla.edu, pengyu.ut <at> gmail.com
Subject: Re: bug#22656: How to grep two patterns in a line with correct
 coloring?
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





This bug report was last modified 9 years and 121 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.