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.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 22656 in the body.
You can then email your comments to 22656 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-grep <at> gnu.org:
bug#22656; Package grep. (Sun, 14 Feb 2016 00:11:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Peng Yu <pengyu.ut <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-grep <at> gnu.org. (Sun, 14 Feb 2016 00:11:02 GMT) Full text and rfc822 format available.

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

From: Peng Yu <pengyu.ut <at> gmail.com>
To: bug-grep <at> gnu.org
Subject: How to grep two patterns in a line with correct coloring?
Date: Sat, 13 Feb 2016 18:10:01 -0600
Hi,

I know that I can use this to grep two patterns. But it can not
highlight both patterns.

grep word1 FILE | grep word2

Or I can use the following. But it is cumbersome as I have to type
word1 and word2 twice. And this method is not scale if I want grep
multiple patterns. The coloring can not be just for word1 and word2
only.

grep -E 'word1.*word2|word2.*word1' logs

Could anybody let me know what is the best way to grep two patterns in
a line with correct coloring? Thanks.

-- 
Regards,
Peng




Information forwarded to bug-grep <at> gnu.org:
bug#22656; Package grep. (Sun, 14 Feb 2016 03:14:01 GMT) Full text and rfc822 format available.

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

From: Gary Johnson <garyjohn <at> spocom.com>
To: bug-grep <at> gnu.org
Subject: Re: bug#22656: How to grep two patterns in a line with correct
 coloring?
Date: Sat, 13 Feb 2016 18:28:37 -0800
On 2016-02-13, Peng Yu wrote:
> Hi,
> 
> I know that I can use this to grep two patterns. But it can not
> highlight both patterns.
> 
> grep word1 FILE | grep word2

This seems to work:

    grep --color=always word1 FILE | grep word2

Regards,
Gary





Reply sent to Paul Eggert <eggert <at> cs.ucla.edu>:
You have taken responsibility. (Mon, 11 Apr 2016 04:46:02 GMT) Full text and rfc822 format available.

Notification sent to Peng Yu <pengyu.ut <at> gmail.com>:
bug acknowledged by developer. (Mon, 11 Apr 2016 04:46:02 GMT) Full text and rfc822 format available.

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

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Peng Yu <pengyu.ut <at> gmail.com>
Cc: 22656-done <at> debbugs.gnu.org
Subject: Re: How to grep two patterns in a line with correct coloring?
Date: Sun, 10 Apr 2016 21:45:49 -0700
This works for me:

grep --color=always -E 'word1|word2'




Information forwarded to bug-grep <at> gnu.org:
bug#22656; Package grep. (Mon, 11 Apr 2016 05:40:02 GMT) Full text and rfc822 format available.

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





Information forwarded to bug-grep <at> gnu.org:
bug#22656; Package grep. (Mon, 11 Apr 2016 05:55:01 GMT) Full text and rfc822 format available.

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

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Zev Weiss <zev <at> bewilderbeest.net>, 22656 <at> debbugs.gnu.org,
 pengyu.ut <at> gmail.com
Subject: Re: bug#22656: How to grep two patterns in a line with correct
 coloring?
Date: Sun, 10 Apr 2016 22:54:21 -0700
Zev Weiss wrote:
> 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)

I interpreted the original question the other way. Now that I reread it, it 
appears to be ambiguous.

This bug report does have the flavor of someone trying to use debbugs.gnu.org to 
solve a homework problem, which means I should probably stop now....




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Mon, 09 May 2016 11:24:05 GMT) Full text and rfc822 format available.

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

Previous Next


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