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.

2

2 Answers 2

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.

6
  • What's a nice way to get grep to run from the new location? an alias?
    – Steven Lu
    May 2, 2012 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 3, 2012 at 6:20
  • 2
  • 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, 2021 at 15:00
  • @mustafa Thanks for the note. I fixed the answer.
    – slhck
    Jan 21, 2021 at 20:01
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .