GNU bug report logs - #55641
Using colours with grep

Previous Next

Package: grep;

Reported by: goncholden <goncholden <at> protonmail.com>

Date: Wed, 25 May 2022 20:49:01 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 55641 in the body.
You can then email your comments to 55641 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#55641; Package grep. (Wed, 25 May 2022 20:49:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to goncholden <goncholden <at> protonmail.com>:
New bug report received and forwarded. Copy sent to bug-grep <at> gnu.org. (Wed, 25 May 2022 20:49:01 GMT) Full text and rfc822 format available.

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

From: goncholden <goncholden <at> protonmail.com>
To: "bug-grep <at> gnu.org" <bug-grep <at> gnu.org>
Subject: Using colours with grep
Date: Wed, 25 May 2022 20:48:49 +0000
Have been using grep to colour the filename printed by tail using grep, by setting GREP_COLOR.  However the setting does not work with GREP_COLORS.

Additionally, I could not set colour values with tput commands.

tail -v -n "$tm" $file | GREP_COLOR='01;32' $_GREP -e ^ -e '^==> .* <==$'

awk and sed work better with colour settings, including settings with tput.

Cholden





Information forwarded to bug-grep <at> gnu.org:
bug#55641; Package grep. (Fri, 27 May 2022 02:18:01 GMT) Full text and rfc822 format available.

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

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: goncholden <goncholden <at> protonmail.com>
Cc: 55641 <at> debbugs.gnu.org
Subject: Re: bug#55641: Using colours with grep
Date: Thu, 26 May 2022 19:16:58 -0700
On 5/25/22 13:48, goncholden via Bug reports for GNU grep wrote:
> Have been using grep to colour the filename printed by tail using grep, by setting GREP_COLOR.  However the setting does not work with GREP_COLORS.
> 
> Additionally, I could not set colour values with tput commands.
> 
> tail -v -n "$tm" $file | GREP_COLOR='01;32' $_GREP -e ^ -e '^==> .* <==$'

Sorry, I'm not following. Can you give a test case that I can reproduce 
here?




Information forwarded to bug-grep <at> gnu.org:
bug#55641; Package grep. (Fri, 27 May 2022 03:43:01 GMT) Full text and rfc822 format available.

Message #11 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#55641: Using colours with grep
Date: Thu, 26 May 2022 20:27:27 -0700
On 2022-05-26, Paul Eggert wrote:
> On 5/25/22 13:48, goncholden via Bug reports for GNU grep wrote:
> >Have been using grep to colour the filename printed by tail using
> >grep, by setting GREP_COLOR.  However the setting does not work
> >with GREP_COLORS.
> >
> >Additionally, I could not set colour values with tput commands.
> >
> >tail -v -n "$tm" $file | GREP_COLOR='01;32' $_GREP -e ^ -e '^==> .* <==$'
> 
> Sorry, I'm not following. Can you give a test case that I can
> reproduce here?

I think the problem is that the OP has used GREP_COLORS to specify
the colors that grep uses, and has then tried to override the
default colors by specifying GREP_COLOR on the command line.  But,
as the man page says, in the GREP_COLOR section,

    The mt, ms, and mc capabilities of GREP_COLORS have priority
    over it.

So the GREP_COLOR setting has no effect when GREP_COLORS is also
used, at least when GREP_COLORS contains the ms capability.

There are a number of solutions to this, depending on what the OP is
trying to achieve with the other values in GREP_COLORS.  One is to
clear GREP_COLORS in their command line, e.g.,

    tail -v -n "$tm" $file | GREP_COLORS= GREP_COLOR='01;32' $_GREP -e ^ -e '^==> .* <==$'

Another would be to set the ms capability in GREP_COLORS instead of
using GREP_COLOR, e.g.,

    tail -v -n "$tm" $file | GREP_COLORS='ms=01;32' $_GREP -e ^ -e '^==> .* <==$'

For a demonstration of the problem, try the following.

    $ man grep | grep prevents

The result is a single line of the man page with "prevents"
highlighted in the default red.

    $ man grep | GREP_COLOR='01;32' grep prevents

This result is the same except that "prevents" is in green.

    $ export GREP_COLORS='ms=01:33'
    $ man grep | grep prevents
    $ man grep | GREP_COLOR='01;32' grep prevents

Now, both results have "prevents" in yellow, which agrees with the
man page.  However, the OP expected the GREP_COLOR setting on the
command line to override the GREP_COLORS setting in the environment,
which it doesn't.

Caveats:

- I am assuming that $_GREP is grep.
- I don't understand the tput problem.
- I don't know anything about grep colors other than what I just
  read in the man page.  The question just intrigued me.

Regards,
Gary





Information forwarded to bug-grep <at> gnu.org:
bug#55641; Package grep. (Sat, 28 May 2022 17:24:02 GMT) Full text and rfc822 format available.

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

From: goncholden <goncholden <at> protonmail.com>
To: Gary Johnson <garyjohn <at> spocom.com>
Cc: 55641 <at> debbugs.gnu.org
Subject: Re: bug#55641: Using colours with grep
Date: Sat, 28 May 2022 17:22:42 +0000
I was trying to change the colours used for matching temporarily.
But GREP_COLORS is global in nature.


------- Original Message -------
On Friday, May 27th, 2022 at 3:27 PM, Gary Johnson <garyjohn <at> spocom.com> wrote:


> On 2022-05-26, Paul Eggert wrote:
>
> > On 5/25/22 13:48, goncholden via Bug reports for GNU grep wrote:
> >
> > > Have been using grep to colour the filename printed by tail using
> > > grep, by setting GREP_COLOR. However the setting does not work
> > > with GREP_COLORS.
> > >
> > > Additionally, I could not set colour values with tput commands.
> > >
> > > tail -v -n "$tm" $file | GREP_COLOR='01;32' $_GREP -e ^ -e '^==> .* <==$'
> >
> > Sorry, I'm not following. Can you give a test case that I can
> > reproduce here?
>
>
> I think the problem is that the OP has used GREP_COLORS to specify
> the colors that grep uses, and has then tried to override the
> default colors by specifying GREP_COLOR on the command line. But,
> as the man page says, in the GREP_COLOR section,
>
> The mt, ms, and mc capabilities of GREP_COLORS have priority
> over it.
>
> So the GREP_COLOR setting has no effect when GREP_COLORS is also
> used, at least when GREP_COLORS contains the ms capability.
>
> There are a number of solutions to this, depending on what the OP is
> trying to achieve with the other values in GREP_COLORS. One is to
> clear GREP_COLORS in their command line, e.g.,
>
> tail -v -n "$tm" $file | GREP_COLORS= GREP_COLOR='01;32' $_GREP -e ^ -e '^==> .* <==$'
>
>
> Another would be to set the ms capability in GREP_COLORS instead of
> using GREP_COLOR, e.g.,
>
> tail -v -n "$tm" $file | GREP_COLORS='ms=01;32' $_GREP -e ^ -e '^==> .* <==$'
>
>
> For a demonstration of the problem, try the following.
>
> $ man grep | grep prevents
>
> The result is a single line of the man page with "prevents"
> highlighted in the default red.
>
> $ man grep | GREP_COLOR='01;32' grep prevents
>
> This result is the same except that "prevents" is in green.
>
> $ export GREP_COLORS='ms=01:33'
> $ man grep | grep prevents
> $ man grep | GREP_COLOR='01;32' grep prevents
>
> Now, both results have "prevents" in yellow, which agrees with the
> man page. However, the OP expected the GREP_COLOR setting on the
> command line to override the GREP_COLORS setting in the environment,
> which it doesn't.
>
> Caveats:
>
> - I am assuming that $_GREP is grep.
> - I don't understand the tput problem.
> - I don't know anything about grep colors other than what I just
> read in the man page. The question just intrigued me.
>
> Regards,
> Gary
>
>




Information forwarded to bug-grep <at> gnu.org:
bug#55641; Package grep. (Sat, 28 May 2022 17:38:01 GMT) Full text and rfc822 format available.

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

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Gary Johnson <garyjohn <at> spocom.com>
Cc: 55641 <at> debbugs.gnu.org, goncholden <goncholden <at> protonmail.com>
Subject: Re: bug#55641: Using colours with grep
Date: Sat, 28 May 2022 10:36:57 -0700
On 5/26/22 20:27, Gary Johnson wrote:
>      $ export GREP_COLORS='ms=01:33'
>      $ man grep | grep prevents
>      $ man grep | GREP_COLOR='01;32' grep prevents
> 
> Now, both results have "prevents" in yellow, which agrees with the
> man page.  However, the OP expected the GREP_COLOR setting on the
> command line to override the GREP_COLORS setting in the environment,
> which it doesn't.

Thanks, your explanation let me see the problem.

Since GREP_COLOR is deprecated, I doubt whether we should be changing 
grep to support this obsolescent usage in a different way. When it comes 
to obsolescent features, the only thing we're likely to change is to 
remove them entirely (as any change will disrupt somebody...).




Information forwarded to bug-grep <at> gnu.org:
bug#55641; Package grep. (Sat, 28 May 2022 18:20:01 GMT) Full text and rfc822 format available.

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

From: goncholden <goncholden <at> protonmail.com>
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: Gary Johnson <garyjohn <at> spocom.com>, 55641 <at> debbugs.gnu.org
Subject: Re: bug#55641: Using colours with grep
Date: Sat, 28 May 2022 18:19:25 +0000
------- Original Message -------
On Sunday, May 29th, 2022 at 5:36 AM, Paul Eggert <eggert <at> cs.ucla.edu> wrote:


> On 5/26/22 20:27, Gary Johnson wrote:
>
> > $ export GREP_COLORS='ms=01:33'
> > $ man grep | grep prevents
> > $ man grep | GREP_COLOR='01;32' grep prevents
> >
> > Now, both results have "prevents" in yellow, which agrees with the
> > man page. However, the OP expected the GREP_COLOR setting on the
> > command line to override the GREP_COLORS setting in the environment,
> > which it doesn't.
>
>
> Thanks, your explanation let me see the problem.
>
> Since GREP_COLOR is deprecated, I doubt whether we should be changing
> grep to support this obsolescent usage in a different way. When it comes
> to obsolescent features, the only thing we're likely to change is to
> remove them entirely (as any change will disrupt somebody...).

I agree on removing GREP_COLOR entirely.  Awk and Sed have convenient ways of temporarily changing colours.

sga="$(tput setaf 46)"
sgr="$(tput sgr0)"

sed -r $"s/^==> .* <==$/${sga}&${sgr}/"

Would be a good improvement if one can change the colour temporarily from the default.  This would help in sectioning matches, particularly when multiple files are involved.





Information forwarded to bug-grep <at> gnu.org:
bug#55641; Package grep. (Sat, 28 May 2022 18:39:02 GMT) Full text and rfc822 format available.

Message #23 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#55641: Using colours with grep
Date: Sat, 28 May 2022 11:38:26 -0700
On 2022-05-28, goncholden wrote:
> I was trying to change the colours used for matching temporarily.
> But GREP_COLORS is global in nature.

An environment variable such as GREP_COLORS set by itself on the
command line persists in the environment after it has been set.  An
environment variable set on the same line as a command, such as

    GREP_COLORS='ms=01;32' grep this_pattern that_file

affects only the environment of that command, persists only for the
duration of that command, and does not affect the shell's
environment.

That may not be completely accurate, but it's close.

So, setting GREP_COLORS on the grep command line as above will
affect only that command and will not affect the "global" setting,
as you put it.

See the ENVIRONMENT section of the bash(1) man page.

Regards,
Gary





Information forwarded to bug-grep <at> gnu.org:
bug#55641; Package grep. (Sun, 29 May 2022 00:18:02 GMT) Full text and rfc822 format available.

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

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: goncholden <goncholden <at> protonmail.com>
Cc: Gary Johnson <garyjohn <at> spocom.com>, 55641 <at> debbugs.gnu.org
Subject: Re: bug#55641: Using colours with grep
Date: Sat, 28 May 2022 17:17:34 -0700
[Message part 1 (text/plain, inline)]
On 5/28/22 11:19, goncholden wrote:
> I agree on removing GREP_COLOR entirely.

Sounds good to me too. Proposed patch attached. I haven't installed 
this, as I'd like Jim's opinion (we're reasonably close to a release I 
think).
[0001-grep-deprecate-GREP_COLOR.patch (text/x-patch, attachment)]

Information forwarded to bug-grep <at> gnu.org:
bug#55641; Package grep. (Sun, 29 May 2022 15:42:02 GMT) Full text and rfc822 format available.

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

From: Jim Meyering <jim <at> meyering.net>
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: Gary Johnson <garyjohn <at> spocom.com>, 55641 <at> debbugs.gnu.org,
 goncholden <goncholden <at> protonmail.com>
Subject: Re: bug#55641: Using colours with grep
Date: Sun, 29 May 2022 08:40:50 -0700
On Sat, May 28, 2022 at 5:18 PM Paul Eggert <eggert <at> cs.ucla.edu> wrote:
> On 5/28/22 11:19, goncholden wrote:
> > I agree on removing GREP_COLOR entirely.
>
> Sounds good to me too. Proposed patch attached. I haven't installed
> this, as I'd like Jim's opinion (we're reasonably close to a release I
> think).

Thanks for writing that.

Yes, I'm close to making a release, indeed, but I do like this change
and it is only a sometimes-triggered warning, so please go ahead.
Adding a test for the new behavior would be nice.




Information forwarded to bug-grep <at> gnu.org:
bug#55641; Package grep. (Sun, 29 May 2022 23:45:01 GMT) Full text and rfc822 format available.

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

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Jim Meyering <jim <at> meyering.net>
Cc: Gary Johnson <garyjohn <at> spocom.com>, 55641 <at> debbugs.gnu.org,
 goncholden <goncholden <at> protonmail.com>
Subject: Re: bug#55641: Using colours with grep
Date: Sun, 29 May 2022 16:44:25 -0700
[Message part 1 (text/plain, inline)]
On 5/29/22 08:40, Jim Meyering wrote:
> Adding a test for the new behavior would be nice.

OK, thanks, I installed the new warning and followed up with the 
attached patch to test it.
[0001-tests-new-test-color-colors.patch (text/x-patch, attachment)]

bug closed, send any further explanations to 55641 <at> debbugs.gnu.org and goncholden <goncholden <at> protonmail.com> Request was from Paul Eggert <eggert <at> cs.ucla.edu> to control <at> debbugs.gnu.org. (Sat, 02 Jul 2022 22:16:01 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 31 Jul 2022 11:24:06 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 269 days ago.

Previous Next


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