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

Match 2 strings in same line

by hmb104 (Sexton)
on Nov 25, 2013 at 16:52 UTC ( #1064245=perlquestion: print w/replies, xml ) Need Help??
hmb104 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to match 2 strings in the same line. If BOTH exists then push that line into my array.

if($line =~ m/$logsIncDate/ && m/SW_MATM-4-MACFLAP_NOTIF/){ push(@eigrp, "$line\n");


Replies are listed 'Best First'.
Re: Match 2 strings in same line
by davido (Archbishop) on Nov 25, 2013 at 16:57 UTC

    The problem with your code is that you are not binding the second match to $line, so it implicitly binds to $_, which isn't what you want. The simplest change would be this:

    push @eigrp, "$line\n" if $line =~ m/$logsIncDate/ && $line =~ m/SW_MATM-4-MACFLAP_NOTIF/;

    Also, if you're just going to be appending a newline to your output, maybe (I don't know because I haven't seen the bigger picture) you could avoid that need by not chomping your input. And if your while loop is trivial, you could use implicit "$_":

    while( <$fh> ) { push @eigrp, $_ if /$logsIncDate/ && /SW_MATM-4-MACFLAP-NOTIF/; }


      Thanks for your reply Dave, but I have already tried all different ways of doing the match statement.

      The above statement should match this line (all txt below is one line), but it doesn't:

      Nov 25 10:59:13 1410: Nov 25 16:59:12.724: %SW +_MATM-4-MACFLAP_NOTIF: Host 30ff.0c00.d4be in vlan 162 is flapping be +tween port Fa0/13 and port Fa0/10
        What's in $logsIncDate? The most obvious case is that your string contains Metacharacters, and so they need to be escaped:
        if($line =~ m/\Q$logsIncDate\E/ and $line =~ m/SW_MATM-4-MACFLAP_NOTIF +/){ push(@eigrp, "$line\n"); }

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        Yes, I was only able to answer based on the code you showed. The problem with the code you showed was the missing $line from the second regexp match. If there's still a problem after applying that change (which is still necessary), then you will have to move on to other issues, such as using \Q at the start of the first regexp.


Re: Match 2 strings in same line
by Eily (Prior) on Nov 25, 2013 at 17:06 UTC

    I don't know what's inside $logsIncDate, but you might want to escape the special characters (like the dot), to be sure to match it exactly. m<\Q$logsIncDate>;. See quotemeta.

      my $logsIncDate = `date +"%b %d %H"`;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1064245]
Approved by davido
Front-paged by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2018-06-22 04:36 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.