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

9

3

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.

Steven Lu

Posted 2012-05-02T04:41:24.360

Reputation: 2 818

This still work? --> http://superuser.com/questions/416835/how-can-i-grep-with-color-in-mac-os-xs-terminal

– emgee – 2012-05-02T04:42:54.077

I just came from there. I did exactly what was described. Just one color got set. – Steven Lu – 2012-05-02T04:45:25.713

Answers

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, but its policy is not to offer duplicates for system tools. There's an alternative repository for these dupes, but you have to "tap" it first:

brew tap homebrew/dupes/

Then you can install the new grep (version 2.11 as of May 2012):

brew install homebrew/dupes/grep

Be aware that /usr/bin still comes first in your PATH, so you need /usr/local/bin/grep for the new version.

slhck

Posted 2012-05-02T04:41:24.360

Reputation: 182 472

What's a nice way to get grep to run from the new location? an alias? – Steven Lu – 2012-05-02T22:14:11.013

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 – 2012-05-03T06:20:49.770

2

@StevenLu, see Amending PATH so that /usr/local/bin is ahead of /usr/bin.

– Chris Page – 2012-05-03T06:31:52.237

1

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

Ignacio Vazquez-Abrams

Posted 2012-05-02T04:41:24.360

Reputation: 100 516