9

I am used to grep being able to color the filename, the line number, and the match itself. These three ought to be different colors. This works flawlessly on a Linux terminal and even MinGW on Windows, but on OS X even if I set GREP_COLOR I can only get color on the matched result.

Is the version of grep packaged with the OS too old?

10.7.3 on MBA 13" with Terminal.app.

chicks
  • 556
  • 4
  • 14
Steven Lu
  • 3,620
  • 3
  • 35
  • 46

2 Answers2

9

As Ignacio already said, OS X grep is a bit outdated (it's version 2.5.1). You can install the latest GNU grep though.

As always, you can install most missing Linux tools on OS X through Homebrew:

brew install grep

This will install ggrep so as not to override your existing grep. If you want to change that, see the info message:

All commands have been installed with the prefix "g". If you need to use these commands with their normal names, you can add a "gnubin" directory to your PATH from your bashrc like:

PATH="$(brew --prefix)/opt/grep/libexec/gnubin:$PATH"

Further, you can enable a color option and exclude some directories by default, which may make it more useful:

alias grep="ggrep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox}"

Add this to your ~/.bash_profile or whatever shell configuration you are using.

Glorfindel
  • 4,089
  • 8
  • 24
  • 37
slhck
  • 223,558
  • 70
  • 607
  • 592
  • What's a nice way to get grep to run from the new location? an alias? – Steven Lu May 02 '12 at 22:14
  • 2
    @StevenLu, you may want to put `/usr/local/bin` before `/usr/bin`. e.g., you can do this globally by editing `/etc/paths`, or have your shell startup script (e.g., `~/.bash_profile`) edit `PATH` to change the order. – Chris Page May 03 '12 at 06:20
  • 2
    @StevenLu, see [Amending PATH so that /usr/local/bin is ahead of /usr/bin](http://apple.stackexchange.com/q/49389/6883). – Chris Page May 03 '12 at 06:31
  • getting `Error: Invalid tap name 'homebrew/dupes/'` and `Error: homebrew/dupes was deprecated. This tap is now empty and all its contents were either deleted or migrated.` – mustafa Jan 21 '21 at 15:00
  • @mustafa Thanks for the note. I fixed the answer. – slhck Jan 21 '21 at 20:01
  • And use the `--color=auto` option in GNU grep – Organic Addict Mar 17 '21 at 23:14
1

Correct. Multiple colors were first supported in GNU grep 2.5.3.

Ignacio Vazquez-Abrams
  • 111,361
  • 10
  • 201
  • 247