Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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 a log crumbles through the grate...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2018-02-18 22:56 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (257 votes). Check out past polls.