Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^3: trouble parsing log file...

by perl_geoff (Acolyte)
on Nov 20, 2006 at 20:58 UTC ( #585146=note: print w/replies, xml ) Need Help??

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

Thanks, I am about halfway through ch.8 'matching with regular expressions' in my learning perl book. Ok, I tried the following:
$logfile="log.txt"; $error=(/DOWN/); $warn=(/PROBLEM/); $redbutton="\<img src\=\'default_files/perlredblink2\.gif'>"; $greenbutton="\<img src\=\'default_files/perlgreenblink\.gif'>"; $yellowbutton="\<img src\=\'default_files/perlyellowblink\.gif'>"; open LOG, $logfile or die "Cannot open $logfile for read :$!"; @logarray=<LOG>; # dumps all of $logfile into @logarray chomp @logfile; my $got_button = 0; foreach my $line (@logarray) { if ($line eq $error) { $got_button = 1; print $redbutton; last; } elsif ($line eq $warn) { $got_button = 1; print $yellowbutton; last; } } if (not $got_button) { print $greenbutton; }
But it still only displays the green button!

Replies are listed 'Best First'.
Re^4: trouble parsing log file...
by liverpole (Monsignor) on Nov 20, 2006 at 22:06 UTC
    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".

Re^4: trouble parsing log file...
by SheridanCat (Pilgrim) on Nov 20, 2006 at 22:05 UTC
    Okay, let's see what we can do here.

    First off, your assignments to $error and $warn probably aren't doing what you think they're doing. If you "use warnings" and "use strict" at the top of your code, you'll see an error that is telling:

    Use of uninitialized value in pattern match (m//)

    Just set $error = 'down' and $warn = 'problem'. Worry about the regular expression stuff later in the code.

    As someone already mentioned, why not go ahead and set the $got_button variable to $greenbutton before you enter the loop? It's nice to have a default state already setup. Then there's no need to do any gymnastics at the end to set it.

    Inside the loop, you have the right idea. Try using a regular expression there. So the "if" clause would look something like:

    if( $line =~ m/$error/i ){

    I used the "i" modifier to make it case insensitive, just in case someone changes log messages on you some day.

    As someone already pointed out, reading the whole log into memory may not be a very good idea since you're bailing out as soon as you find a red/yellow state, so consider just processing the file a line at a time using <LOG>.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2018-03-24 16:25 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (299 votes). Check out past polls.