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

Re^2: Match 2 strings in same line

by hmb104 (Sexton)
on Nov 25, 2013 at 17:02 UTC ( #1064247=note: print w/ replies, xml ) Need Help??


in reply to Re: Match 2 strings in same line
in thread Match 2 strings in same line

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 10.1.162.11/10.1.162.11 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


Comment on Re^2: Match 2 strings in same line
Download Code
Re^3: Match 2 strings in same line
by kennethk (Monsignor) on Nov 25, 2013 at 17:07 UTC
    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.

      my $logsIncDate = `date +"%b %d %H"`; my @eigrp = (); open SwitchLogs, $logFile or die "Could not open $logFile: $!"; while (<SwitchLogs>) { chomp; my $line = $_; if($line =~ m/$logsIncDate/ && $line =~ m/SW_MATM-4-MACFLAP_NOTIF/ +){ push(@eigrp, "$line\n"); $sendemail = 1; } }

      All I want to do is to match the date/time in a line that contain the specified string. So the above code should catch the below one line:

      Nov 25 10:59:13 10.1.162.11/10.1.162.11 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
        The result of `date +"%b %d %H"` has a trailing newline, which is not present in your string. You need to remove that, perhaps with chomp:
        chomp(my $logsIncDate = `date +"%b %d %H"`);

        Also, why are you chomping your input, only to re-append a newline on storage? Seems like you could avoid unnecessary operations (and more opportunities for confusion and bugs) with

        while (my $line = <SwitchLogs>) { if($line =~ m/$logsIncDate/ && $line =~ m/SW_MATM-4-MACFLAP_NOTIF/ +){ push(@eigrp, $line); $sendemail = 1; } }

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

        Hint: the last character of $logsIncDate is a newline. Maybe you should consider using POSIX::strftime instead


        Enjoy, Have FUN! H.Merijn
Re^3: Match 2 strings in same line
by davido (Archbishop) on Nov 25, 2013 at 17:18 UTC

    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.


    Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-12-27 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls