Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

grep -P

by tomazos (Deacon)
on Jun 25, 2006 at 19:55 UTC ( [id://557464]=perlquestion: print w/replies, xml ) Need Help??

tomazos has asked for the wisdom of the Perl Monks concerning the following question:

In the man page for grep it says:

-P, --perl-regexp Interpret PATTERN as a Perl regular expression.

When I try this I get:

$ grep -P 'whatever' grep: The -P option is not supported

(Using Ubuntu 6.06 Dapper Drake)

Any ideas on the history of this feature? Why is it "not supported"? How do I get a version of grep that supports it?

When the feature works, what is the extent of greps support for Perl regexps? Does it actually use perl, or is it a reimplemented engine?

-Andrew.

Replies are listed 'Best First'.
Re: grep -P
by ambrus (Abbot) on Jun 25, 2006 at 19:58 UTC

    It uses an external library called pcre, and I think the feature is not always compiled in grep.

    If you want to use real perl expressions with grep, an alternative to gnu grep is to use one of the several grep clones implemented in perl, such as mine.

      Interesting...

      $ apt-cache search pcre libpcre3 - Perl 5 Compatible Regular Expression Library - runtime file +s libpcre3-dev - Perl 5 Compatible Regular Expression Library - developm +ent files libpcrecpp0 - Perl 5 Compatible Regular Expression Library - C++ runti +me files pcregrep - grep utility that uses perl 5 compatible regexes. pgrep - Dummy package for transition to pcregrep

      I suppose pcregrep or pgrep might somehow help.

      -Andrew.

Re: grep -P
by Fletch (Bishop) on Jun 25, 2006 at 22:11 UTC

    Keep in mind that even if it's compiled against pcre that it's not going to be able to understand all Perl regexen syntax (things like (?{}), for instance). I always keep a copy of tcgrep lying around.

Re: grep -P
by sgifford (Prior) on Jun 26, 2006 at 01:46 UTC
    If you don't need the more complex command-line options of grep, you can just use perl -ne 'print if (/pat/)'. Even the trickier options usually come down to just a small variation on that theme.
      I guess -R (recurse directory) and -l (print filenames containing matches) are two that I use often. The one liner is a bit of a mouthful.

      But just for fun:

      perl -MFile::Find -ne 'find(& sub { $f = 1; if (/pat/ and $f) { $f = 0 +; print $File::Find::name . "\n" }, "mydir");'

      ...or something like that.

      -Andrew.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://557464]
Approved by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 20:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found