Grep in Mac OS X's terminal -- only displaying one color

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.


Solution 1:

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.

Solution 2:

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