Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

multiple regex logical AND

by bigswifty00000 (Novice)
on Dec 12, 2006 at 18:36 UTC ( #589354=perlquestion: print w/ replies, xml ) Need Help??
bigswifty00000 has asked for the wisdom of the Perl Monks concerning the following question:

multiple regex logical AND

Hey All!

I am trying to check a config file for the existence and settings of two parameters.

I believe it requires a logical AND.

I realize that this is checking for an AND on the same line,

perl -nle 'print $ARGV if !$seen{$ARGV}++ ;print if /^JRECOV="yes"/|/^TEOJ=-w"/' *

ALSO printing the filename only if BOTH conditions are met.

I can't seem to find the regex that will work for this, any help would be greatly appreciated.

Thanks!

BigSwifty00000

Sorry, I realized my LAST test was with an OR, after I posted.

Carry on!

BigSwifty0000

I corrected the missing " and tried this (this file has the 2 params set to what I'm searching for)

perl -nle 'print $ARGV if !$seen{$ARGV}++ ;print if /^JRECOV="yes"/&&/^TEOJ="-w"/' ss1lab02

AND

perl -n00 -le 'print $ARGV if !$seen{$ARGV}++ ;print if /^JRECOV="yes"/ms &&/^TEOJ="-w"/ms' ss1lab02

Neither is returning the lines (filename is returned)

Comment on multiple regex logical AND
Re: multiple regex logical AND
by shmem (Canon) on Dec 12, 2006 at 19:05 UTC
    You don't want bitwise or (|) but logical and (&&):
    perl -nle 'print $ARGV if !$seen{$ARGV}++ ;print if /^JRECOV="yes"/&&/ +^TEOJ=-w"/'

    Hmm.. but is it possible that two strings match the beginning of a line? Yes, if you slurp the file or use paragraph mode and use the m and s modifiers (see perlre):

    perl -n00 -le 'print $ARGV if !$seen{$ARGV}++ ;print if /^JRECOV="yes" +/ms &&/^TEOJ=-w"/ms'

    --shmem

    update: corrected glitch with || and &&

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      corrected the missing " and tried this (this file has the 2 params set to what I'm searching for) perl -nle 'print $ARGV if !$seen{$ARGV}++ ;print if /^JRECOV="yes"/&&/^TEOJ="-w"/' ss1lab02 AND perl -n00 -le 'print $ARGV if !$seen{$ARGV}++ ;print if /^JRECOV="yes"/ms &&/^TEOJ="-w"/ms' ss1lab02 Neither is returning the lines (filename is returned)

      corrected the missing " and tried this (this file has the 2 params set to what I'm searching for)

      perl -nle 'print $ARGV if !$seen{$ARGV}++ ;print if /^JRECOV="yes"/&&/^TEOJ="-w"/' ss1lab02

      AND

      perl -n00 -le 'print $ARGV if !$seen{$ARGV}++ ;print if /^JRECOV="yes"/ms &&/^TEOJ="-w"/ms' ss1lab02

      Neither is returning the lines (filename is returned)

        Please see I know what I mean. Why don't you?

        What's your ss1lab02 file like? mine is:

        # sample input file for node 589354 # hope this helps. no=good JRECOV="yes" TEOJ="-w" foo=bar quux=blorfl

        Oneliner, and output:

        bash$ perl -n00 -le 'print $ARGV if !$seen{$ARGV}++ ;print if /JRECOV= +"yes"/ms && /TEOJ="-w"/ms' ss1lab02 ss1lab02 no=good JRECOV="yes" TEOJ="-w"

        Tired of guessing,
        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: multiple regex logical AND
by lin0 (Curate) on Dec 12, 2006 at 19:22 UTC
      But how can /^JRECOV="yes"/ and /^TEOJ="-w"/ both be true at the same time?

      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

        Multiline with m/.../m, but that isn't specified, so liverpole++.

        --MidLifeXis

Re: multiple regex logical AND
by liverpole (Monsignor) on Dec 12, 2006 at 19:28 UTC
    Hi bigswifty00000,

    A couple of other points, additionally to what shmem has already pointed out:

    1. I think you want something along the lines of $ARGV[0], as @ARGV is the argument vector (read "array") passed to your Perl script, -not- a scalar.
    2. It looks like you're missing a quotation mark in:  /^TEOJ=-w"/ (see where?)
    3. You can use '|' in regular expressions like this:  if /^(JRECOV="yes"|TEOJ="-w")/.&nbsp;&nbsp;But it's usually faster to do it with multiple conditionals like:&nbsp;&nbsp;<c>if ($_ eq 'JRECOV="yes"' or $_ eq 'TEOJ="-w"'), especially if you have lots of patterns to match.

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: multiple regex logical AND
by ikegami (Pope) on Dec 12, 2006 at 20:41 UTC

    As previously noted, /^JRECOV="yes"/ && /^TEOJ="-w"/ is no good.

    The desired condition is "Were JRECOV and TEOJ seen in the file?" Notice the past tense. The "and" needs to be done at the end of the file.

    while (<>) { if ($seen) { $seen = 1; print "$ARGV: "; } $JRECOV = 1 if /^JRECOV="yes"/; $TEOJ = 1 if /^TEOJ="-w"/; if (eof) { # Not eof()! print $JRECOV && $TEOJ ? "yes" : "no", "\n"; $seen = 0; $JRECOV = 0; $TEOJ = 0; } }

      thanks very much for your time and effort.

      I was trying to keep this to a line since this comes up all the time for me where I'd like to do a "grep like" search.

      I still may end up using your suggestion, if it can't be done in a one-liner.

      BigSwifty00000

        You needed far more basic help, so I went for readability.

        I'm sure it can be golfed to fit into a respectable (although lengthy) one liner, but I left that as an exercise for the reader.

Re: multiple regex logical AND
by brian_d_foy (Abbot) on Dec 12, 2006 at 21:25 UTC

    If you are playing with configuration files, a configuration file module will do most of the work and hide a lot of the complexity. Users like to do odd thing such as using whitespace around elements, using different quotes, and other things which the modules can handle and make the regexes unruly:

    #!/usr/bin/perl use ConfigReader::Simple; my $file = "/Users/brian/Desktop/config.txt"; my $config = ConfigReader::Simple->new( $file ); print "Has both JRECOV and TEOJ\n" if( $config->get( "JRECOV" ) eq 'yes' && $config->get( "TEOJ" ) eq '-w' );
    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review

      Very interesting! Thanks! I'd still like to be able to do a "grep like thing" for other searches

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-10-23 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls