Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re^4: trouble parsing log file...

by liverpole (Monsignor)
on Nov 20, 2006 at 22:06 UTC ( #585156=note: print w/replies, xml ) Need Help??

in reply to Re^3: trouble parsing log file...
in thread trouble parsing log file...

You need to look at my example more closely.

With regular expressions, you can't just set a string to $error=(/DOWN/), and then check whether your line matches it with if ($line eq $error) {.

You need to use the regular expression match operator =~ to see whether your string matches the pattern:

if ($line =~ /DOWN/) { # ... } elsif ($line =~ /PROBLEM/) { # ... }

Now, it's true that you can abstract some of this.  For example, you could assign string variables, and then use pattern matching on those variables:

my $error = "DOWN"; my $warn = "PROBLEM"; # ... and later ... if ($line =~ /$error/) { # ... } elsif ($line =~ /$warn/) { # ... }

Which in the above case should work fine.

But be aware that, if your variable contains any characters which have special meaning within regular expressions, those meanings will continue to apply.

For example, if the string you're trying to match is literally "Server ... DOWN", and you attempt to assign and match against that string with:

my $error = "Server ... DOWN"; if ($line =~ /$error/) { # conditional clause }

then you'll be using the special meaning of "." within a regular expression (that of matching any character other than end-of-line), and your conditional will thus also match strings like the hypothetical "Server Not DOWN".


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://585156]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2017-09-23 04:19 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (270 votes). Check out past polls.