Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

pcre grep

by siffland (Novice)
on Jun 10, 2010 at 13:32 UTC ( #844003=perlquestion: print w/replies, xml ) Need Help??
siffland has asked for the wisdom of the Perl Monks concerning the following question:

I need some help with pcre grep, I am unsure if it is OK to post for help here, so please tell me if it is not.

Here is what i am trying to accomplish. I need to grep anything out of the /etc/passwd with a UID of 205 or greater and that does not start with a - . The software we are using, cfengine, requires the use of a pcre. So far i have:

pcregrep -v ".*:.*:(-|1)?[0-9]{1,2}:.*:.*:.*" /etc/passwd

But that yields anything above 200, any help would be appreciated.

If anyone know any great webpages for pcre grep I would love to be pointed that direction. I am not that great with perl regular expressions, but would like to learn more.



Replies are listed 'Best First'.
Re: pcre grep
by duelafn (Vicar) on Jun 10, 2010 at 14:42 UTC

    Well, perl can do this easily enough:

    perl -naF: -e 'print if $F[2] >= 205' /etc/passwd

    See documentation for -a in perlrun

    Good Day,

Re: pcre grep
by linuxer (Curate) on Jun 10, 2010 at 20:43 UTC

    I think, you could have a look at the official perldoc to get information about Perl's regular expressions:

    perlretut - Tutorial

    perlrequick - Quick Reference

    perlre - Regex documentation

    And not to forget the pcre man pages:

    If you want/have to stick to pcregrep, I suggest something like:

    pcregrep -v "^[^:]*:[^:]*:-?(1?\d?\d|20[0-4]):" /etc/passwd

    It (simplified) assumes/uses:

    • the pattern is anchored at beginning of line (^)
    • "login name" field must not contain a colon
    • "password" field must not contain a colon
    • "UID" field:
      • leading optional "-"
      • grouped alternatives:
        • number between 0 and 199 OR
        • number between 200 and 204
    • don't care about the rest of the line

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://844003]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2018-01-22 09:19 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (233 votes). Check out past polls.