Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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!


Comment on Re^3: trouble parsing log file...
Download Code
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>.

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".


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-12-25 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (160 votes), past polls