Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Regex returns match the first time. It returns everything thereafter

by guitarplayer68 (Novice)
on Nov 15, 2013 at 14:58 UTC ( #1062758=perlquestion: print w/ replies, xml ) Need Help??
guitarplayer68 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Iíve got a regex issue I am trying to figure out. The situation is I need to pull a file from several servers and return everything past a particular point in that file. The module selection is limited to what is available in a default install. The problem I am having is the regex works the first time. It only returns what I requested, but when it moves on to the next server, and every server thereafter, it returns the entire file. Iíve banged my head at this for the past day or so and just canít seem to get anywhere. The code is below, generalized for this example.

#!/usr/bin/perl use strict; my $server; my $usrallow = "/etc/opt/quest/vas/users.allow"; my @ssh; my $line; foreach $server(@ARGV) { print "getting cgrpfile from $server\n"; @ssh = `ssh $server /bin/cat $usrallow `; foreach $line (@ssh) { if ($line =~ /line/../\\Z/) { print $line; } } }

The output is below, again somewhat generalized

getting cgrpfile from x.x.x.5 customizations must be after this line. customgrp1 customgrp2 customgrp3 getting cgrpfile from x.x.x.6 #START: Core Groups defaultgrp1 defaultgrp2 defaultgrp3 defaultgrp4 defaultgrp5 defaultgrp6 customizations must be after this line customgrp1 customgrp7 customgrp8 getting cgrpfile from x.x.x.7 #START: Core Groups defaultgrp1 defaultgrp2 defaultgrp3 defaultgrp4 defaultgrp5 defaultgrp6 customizations must be after this line customgrp5 customgrp12 customgrp10

Comment on Regex returns match the first time. It returns everything thereafter
Select or Download Code
Re: Regex returns match the first time. It returns everything thereafter
by RMGir (Prior) on Nov 15, 2013 at 18:45 UTC
    What do you think
    /line/../\\Z/
    does?

    What it actually parses out as is this:

    perl -MO=Deparse -e'$line =~ /line/../\\Z/' $line =~ /line/ .. /\\Z/;
    That means it's a flip-flip (perlop for more info on that), which means it stays true after the first regex matches until the 2nd matches (which it probably never does).

    What were you trying to do?


    Mike

      Hi Mike,
      What I think the regex does is this. It matches from 'line' to the end of the string. As for what I was trying to accomplish. I'm trying to grab the contents of a file which lives across several servers but only return the lines in the file which occure after the line, "customizations must be after this line." I'll read up on perlop this weekend and see what i can learn.
      Thank you for your reply.

Re: Regex returns match the first time. It returns everything thereafter
by kcott (Abbot) on Nov 15, 2013 at 18:47 UTC

    G'day guitarplayer68,

    The base problem is that when /line/ matches, the range operator becomes TRUE; thereafter, it never becomes FALSE. See perlop: Range Operators for a full description of the flip-flop boolean states of this operator.

    The more specific problem (i.e. why the operator doesn't become FALSE), would be that /\\Z/ never matches. What that matches is a literal backslash (\\) followed by a capital zed (Z). I suspect that's not what you meant. I further suspect that you're perhaps confused with \Z which matches the end of a string (possibly before a newline if one exists). See Assertions under perlre: Regular Expressions. Unfortunately, \Z is not what you want either.

    From the data you've shown, there doesn't appear to be anything you can use to match the last line in each file. Given this, you might be better off changing your code to something with logic along these lines:

    #!/usr/bin/env perl -l use strict; use warnings; my @test_data = ( [qw{discardA1 discardA2 lineA wantedA1 wantedA2}], [qw{discardB1 discardB2 lineB wantedB1 wantedB2}], [qw{discardC1 discardC2 lineC wantedC1 wantedC2}], ); for (@test_data) { my @ssh = @$_; my $wanted_found = 0; for my $line (@ssh) { if (! $wanted_found) { $wanted_found = $line =~ /line/; } else { print $line; } } }

    Output:

    wantedA1 wantedA2 wantedB1 wantedB2 wantedC1 wantedC2

    If you also wanted the line that matched /line/, you can change that logic to this:

    #!/usr/bin/env perl -l use strict; use warnings; my @test_data = ( [qw{discardA1 discardA2 lineA wantedA1 wantedA2}], [qw{discardB1 discardB2 lineB wantedB1 wantedB2}], [qw{discardC1 discardC2 lineC wantedC1 wantedC2}], ); for (@test_data) { my @ssh = @$_; my $wanted_found = 0; for my $line (@ssh) { if (! $wanted_found) { next unless $wanted_found = $line =~ /line/; } print $line; } }

    Output:

    lineA wantedA1 wantedA2 lineB wantedB1 wantedB2 lineC wantedC1 wantedC2

    -- Ken

      Just one word to say that I tried earlier to post a message saying that your second term of the flip flop operator is probably never matched, so that the operator remains always true, but it was impossible to post a message at the time. So, basically, I fully agree with previous comments.

      Ken
      I didn't notice the extra \ when I was typing this up. Sorry for the confusion.
      It is not that I am trying to match the last line. I am trying to get everything after the line "customizations must be after this line." and ignore the rest.
      Thank you for your reply

Re: Regex returns match the first time. It returns everything thereafter
by AnomalousMonk (Abbot) on Nov 15, 2013 at 21:03 UTC
        if ($line =~ /line/../\\Z/) { ... }

    The  /line/ regex is bound to (i.e., matches against) the  $line scalar. The  /\\Z/ regex is not explicitly bound to any scalar, and so matches against the  $_ default scalar, which is not initialized to anything in the code shown in the OP. So no, the  /\\Z/ regex never matches and the  .. flip-flop operator never flops back to its false state. Doesn't this match against an undefined value produce a warning? Are you using warnings?

      Yes I am using warnings and there are no undefined value messages.

        This could be explained if  $_ had been assigned a defined value by some operation previous to the code shown in the OP.

        The other twist I noticed is that the regex is really  /\Z/ and not  /\\Z/. The former regex matches everything, even undef (although a warning is still generated for a match against undef if warnings are enabled).

Re: Regex returns match the first time. It returns everything thereafter
by Anonymous Monk on Nov 16, 2013 at 00:22 UTC

    You're better off not using the flip-flop operator, but something like this might work:

    for my $server (@ARGV) { print "getting cgrpfile from $server\n"; my @ssh = `ssh $server /bin/cat $usrallow `; for (@ssh, '') { if (/^custom/ .. /^\z/) { print; } } }
      Nope, any empty line will match '/^\z/' and stop the flip-flop.

      There is no end-of-array test possible in a foreach-loop, so no useful flop-condition.

      Using a flip-flop here is nonsense.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        Hi Rolf,
        What would you suggest I use?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-09-16 07:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (157 votes), past polls