6

I'm on OSX, but I suspect this doesn't make a big difference for this question. In my .bash_profile, I aliased grep to get color outputs by default:

alias grep='grep --color=always'

I commonly run searches for content within files in my repositories in ways similar to:

grep --include=*.cpp -Ern . -e "(foo|bar)"

but I often want to further refine the results, typically piping with say grep -v "colorbar".

The problem is that the second grep command then runs on the colored ouput, and doesn't seem to be able to match the exclusion patterns because of this.

Obviously I could run the first search without colors and then everything would work fine, but I would prefer to keep them if there is a way to work around this?

7
  • 1
    looks like a duplicate of unix.stackexchange.com/questions/111899/… . might also take a look at commandlinefu.com/commands/view/3584/… – Dani_l Apr 1 '16 at 17:49
  • 1
    If you don't always want colors, why are you setting it to do that? Do you often want the colors to be preserved through greps? Perhaps you could just create an "uncolored" grep alias for those cases? – Eric Renouf Apr 1 '16 at 17:51
  • @EricRenouf As added with the edit, I'd prefer if the original matches could still be colored after the filter obviously. – Jonathan H Apr 1 '16 at 17:52
  • @Dani_l Indeed this seems to work: grep --include=*.cpp -Ern . -e "(foo|bar)" | sed -E "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | grep -v "colorbar" And it preserves the colors too! :) Should I close the OP then? – Jonathan H Apr 1 '16 at 17:54
  • 2
    @Sh3ljohn , I am curious : How is color getting preserved ? Will not the grep fail if colors are still present ? – Prem Apr 1 '16 at 18:01
2

The simple and obvious answer is:

  1. Change your alias to have --color=auto instead of always
  2. Pipe your grep commands to your heart's content
  3. Use an extra grep command at the end of the pipeline just to color the results you want colored, e.g.:

    grep --include=*.cpp -Ern . -e "(foo|bar)" | grep -v colorbar | grep -E -e "(foo|bar)"
    

This won't color your filenames and file numbers so nicely, however.

Another possible solution is much trickier, but still worth mentioning: You can use pcregrep and a single much fancier regex to handle all your filtering in one regex.

2
  • 1
    Yes, so matching without colors, filtering, and then matching with colors will work. It just feels a bit hacky, doesn't it? – Jonathan H Apr 2 '16 at 15:53
  • 1
    @Sh3ljohn it does indeed. You might consider running the -v test first, so you would only need two grep commands total. There's another consideration about chaining grep commands you may not have considered, which is that the file names become part of the text examined by later grep commands. Possible logic errors therefrom. – Wildcard Apr 2 '16 at 18:45
-1

remove the file /etc/profile.d/colorgrep* With the newer version os linux they are forcing a alias for greg/egrep and fgreg remove these files and it gets back to normal.

1
  • Wouldn't that break color usage in all grep invocations? I'd also recommend against deleting files in /etc that a package manager has installed. – Kusalananda Jan 12 '17 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.