Perl Regex Removed From Grep in Mountain Lion

I realized today that many of my shell scripts were no longer working since I upgraded to OS X 10.8. After digging in to it I found out that they were all bombing out on a common grep command I use for finding specific bits of text with Perl regular expressions.

grep -Po '(?<= ).*?(?= )'

I pulled up the grep man page and couldn’t find the -P switch I had always used for Perl regex. It was definitely removed.

To be completely certain, I jumped on a machine running OS X 10.7.4 to see if it was in the last OS, and sure enough there it was.

I am not sure why it was removed, but it was a great way to use powerful Perl regular expressions in a quick and convenient shell script. I really loved being able to whip up a quick shell script for something and use Patterns for the Perl regex. I know egrep has ‘extended’ regex but I like the Perl syntax. I guess it is time to leave my shell scripts behind and start writing in some more sophisticated scripting languages.

Maybe it wasn’t Apple and it was just apart of its open-source development, I don’t know, but it will be missed by me.

  • Foljs

    How about 

    brew install grep

  • No

    how about ‘perl -pe’

  • J David Lowe

    I recommend ‘ack’: http://betterthangrep.com/

  • Foo

    How about you just install Linux, like every self-respecting developer would?

  • Alexander Berezovskiy

    Absolutely correct:

    $/usr/bin/grep –version
    grep (BSD grep) 2.5.1-FreeBSD

    $ /usr/local/bin/grep –version
    /usr/local/bin/grep (GNU grep) 2.14

    $ /usr/local/bin/grep –help
    ….
    -P, –perl-regexp         PATTERN is a Perl regular expression

  • Bar

    You would be surprised how many of your “self-respecting” developers use macs.

  • Baz

    How about you stop being ignorant and realize those “self-respecting” developers should  be comfortable in any environment and shouldn’t rely on an OS

  • James Wright

    OK, first off, despite the documentation of gnu grep, these are not perl regexes, they are PCRE (perl compatible regular expressions), which is almost, but not quite the same.

    If you need to grep with pcre, use pcregrep.   You shouldn’t have relied on a GNU grep extension that isn’t part of the grep posix spec if you aren’t using Linux.

  • Pingback: Apple kills grep in 10.8 : cybermondy

  • Pingback: Apple kills grep in 10.8 : cyberbullyinginformation

  • Pingback: Apple kills grep in 10.8 : cybermondaydeal

  • ichy

    perl is evil.

    learn python.

  • Emory L

    Prior to 10.8, OS X used GNU grep.  It now uses BSD grep because of the license.  Installation GNU grep will let you use GNU grep’s flags and switches.  It’s a bit awkward, but was bound to happen — wget was replaced with curl for the same reason.  

    If there is a BSD-licensed alternative to a GNU binary, Apple will use it.

  • http://twitter.com/hynek Hynek Schlawack

    It seems that FreeBSD doesn’t support -P either. But there’s good news: homebrew’s grep is GNU grep and therefore supports -P: https://github.com/homebrew/homebrew-dupes/commits/master/grep.rb

    You’ll have to “tap” homebrew-dupes first though.

    HTH

  • AppleWentBad

    How about perl -ne “print $& if //” for grep -Po and perl -ne “print if //” for grep -P?

  • StopClosingMyTags

    I meant: How about perl -ne “print $& if //” for grep -Po and perl -ne “print if //” for grep -P?

  • kenorb

    Is there any bug report about this? It’s very annoying

  • bronek

    Thanks, I’ll not upgrade my OS to Lion! Never!

  • Raine

    Error: No available formula for grep

    Edit: OK, you need to do brew tap homebrew/dupes first.