Thursday, September 26, 2013

Install gnu grep on mac osx

One thing that bugs me about the bsd grep that comes with mac osx is that it doesn't offer you the -P flag. The -P flag will let you use the pcre engine in your regex matches, which is 100x more awesome than using the posix regex of egrep.

Not only that, but did you know gnu grep is significantly faster than the default bsd version? Go ahead and read Why is Gnu Grep So fast? where the author of gnu grep himself chimes in to explain why. Lots of nerdy tidbits in there.

You can install gnu grep on your mac very easily using brew. If you don't have brew you can install it following these instructions.

Installing gnu grep:


# Enable dupe and install
brew tap homebrew/dupes
brew install homebrew/dupes/grep

# Install the perl compatible regular expression library
brew install pcre

# Add the symlink to a place in $PATH
ln -s /usr/local/Cellar/grep/2.14/bin/ggrep /usr/bin/ggrep
# Add an alias
alias grep="ggrep"

# Verify you got it!
$ grep --version

grep (GNU grep) 2.14
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.

# If you want it to be permanent, you can add the alias line to your ~/.bash_profile

# You probably want the alias to stick after reboots
echo 'alias grep="ggrep"' >> ~/.bash_profile

All done!

15 comments:

  1. Thanks for this! Saved me some frustration after installing homebrew grep and expecting the -P flag

    ReplyDelete
  2. When you install homebrew's GNU grep, besides putting a copy into Cellar, it also puts a symlink to it at /usr/local/bin/ggrep.

    This means you do not need to re-alias the system-default "grep" away from Apple's BSD default: you can simply invoke "ggrep".

    Personally I would create a Bash alias of 'grep' against ggrep before I'd muck with the OS symlinks. You're much less likely to affect any existing Apple or third party scripts. (I can't name a case where ggrep would fail if it replaced BSD grep, but that's not the point).

    My shell scripts simply check if 'ggrep' exists in the path; else it prints a warning. This covers my bacon if something I wrote gets copied to another server. I also do this because debugging shell scripts is not something you would ever want to wish upon someone else, and checking the binaries isn't always the first thing folks think of.

    Anyways I found this article to be pretty useful, and being able to -P grep saves me from looking up oddball Perl one-liners.

    ReplyDelete
  3. Not only that, Mac OS X grep _used to_ support "grep -P", prior to Mountain Lion:

    http://www.dirtdon.com/?p=1452

    In fact, if you execute "grep --help", you will still see the P option listed in the usage summary:

    usage: grep [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ]

    So yeah, either Apple or BSD took this feature out at some point.

    I get around this mostly by piping my grep command into "sed".

    ReplyDelete
  4. That's quite ridiculous. It's hard to imagine a change like that being intentional.

    If I can't use the -P flag I'll usually just use a perl one-liner:

    cat file.txt | perl -wnlE '/(text1)*(\d+)/ and print $1,$2'

    ReplyDelete
  5. Finally able to get this working (had to do a bit of tweaking), but now grep doesn't work when run via applescript. Any idea how I can fix the problem?

    ReplyDelete
  6. Sorry, don't worry about it. Turns out I screwed up the initial install, I just reinstalled Mavericks and all is fine. I think I messed up when changing the symlink and wasn't able to fix it. Thanks though.

    ReplyDelete
  7. I followed your directions precisely, including the symlink ln.

    Now, bash no longer recognizes grep, as in:

    -bash: grep: command not found

    I'm afraid that I'll have to reinstall OS X. I'll admit, I'm not much of a shell user, but I assumed that there would be little risk in following your short guide. Please advise.

    ReplyDelete
    Replies
    1. Matt,

      First of all, don't reinstall OSX. No need for that. The error you're seeing means the executable "grep" can't be found in your PATH.

      I would try copying grep over to /usr/bin:

      cp /usr/local/Cellar/grep/2.14/bin/ggrep /usr/bin/grep

      then source your bashrc and try running grep again. If that doesn't work something might be bad with your grep. Download it again and try the steps.

      Delete
    2. I installed Mavericks (before checking back) and grep was back to normal. Then, I followed the steps for install again, and now grep can no longer be found. Thanks for your quick response. I'm posting my Terminal interaction here so that you can see. The latest version is 2.18, so I modified your language to reflect that.

      mcdhcp3-126:~ mateuszek$ brew tap homebrew/dupes
      Warning: Already tapped!
      mcdhcp3-126:~ mateuszek$ brew install homebrew/dupes/grep
      Warning: grep-2.18 already installed
      mcdhcp3-126:~ mateuszek$ ln -fs /usr/local/Cellar/grep/2.18/bin/ggrep `which grep`
      mcdhcp3-126:~ mateuszek$ source ~/.bashrc
      -bash: /Users/mateuszek/.bashrc: No such file or directory
      mcdhcp3-126:~ mateuszek$ grep --version
      -bash: grep: command not found
      mcdhcp3-126:~ mateuszek$ cp /usr/local/Cellar/grep/2.18/bin/ggrep /usr/bin/grep
      mcdhcp3-126:~ mateuszek$ grep --version
      dyld: Library not loaded: /usr/local/lib/libpcre.1.dylib
      Referenced from: /usr/bin/grep
      Reason: image not found
      Trace/BPT trap: 5
      mcdhcp3-126:~ mateuszek$

      Please advise. I kind of wish I had just kept it as it was after the Mavericks install. It was slow, but it worked, and at this point I'm skeptical as to whether or not I'll be able to get things back to normal.

      Delete
    3. Hey Matt,

      Looks like you might be missing the perl regular expression library. I'm surprised brew let you install without it. It may be part of the osx developer tools (https://developer.apple.com/xcode).

      I would try deleting grep. Installing the osx developer tools and also libpcre, then installing grep again.

      To install libpcre:

      brew install pcre

      Delete
  8. This comment has been removed by the author.

    ReplyDelete
  9. symlink to /usr/local/bin on El Capitan. http://apple.stackexchange.com/questions/196224/unix-ln-s-command-not-permitted-in-osx-el-capitan-beta3/196226#196226?newreg=928a83edf91e406e8a15e0ed54b389de

    ReplyDelete
  10. Error: homebrew/dupes was deprecated. This tap is now empty as all its formulae were migrated.

    ReplyDelete
  11. dupes has been deprecated. Now you just `brew install grep`

    ReplyDelete