Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: search log file with multiple word and count number of success

by hipowls (Curate)
on Feb 24, 2008 at 04:21 UTC ( [id://669822]=note: print w/replies, xml ) Need Help??


in reply to Re^2: search log file with multiple word and count number of success
in thread search log file with multiple word and count number of success

  1. Use a regular expression to get only the bits of localtime you are after
    my ($current_date) = map { qr/$_/ } (scalar localtime) =~ /\w+ +(\w+ + +\d+)/;
  2. Rather than put each error in separate quotes I'd rather use qw().
    my @errors = map {qr/$_/} qw( error4 error3 error2 error1 );
    and put them in a meaningful variable.
  3. Since these variables are only used in a regex we might as well precompile them (the maps).
  4. It is good you test that you test that you opened a file, it is better to say why you couldn't
    open my $LOGFILE, '<', $log_file or die "can not open $log_file for writing: $!\n";
  5. Declare variables in as small as scope as possible, that is loop variable get declared as part of the loop, not left floating around. Replace
    $w = 1; while ($w <= @word_search) { # uses $w - 1 to get $word_search[$w - 1] $w++; }
    with
    foreach my $w ( 0 .. $#word_search ) { # uses $w to get $word_search[$w] }
    or better still don't split the line and do away with the loop altogether.
  6. This is where your logic utterly fails. You test the same word for equality with two different words, $pattern_date and "error4". Not only that but the date from the log file is in two separate words since split them apart to get your list of words.

    if ($word_search[$w-1] eq $pattern_date && $word_search[$w-1] eq $arra +y[$count])
    Note you don't initialise $count or ever change its value, you are always testing against the same error.

  7. Use regular expressions to test for the presence of the date and the errors, it is easier than splitting a line into words and iterating over the list.
    if ( $line =~ /$current_date/ ) { foreach my $error (@errors) { # count all the errors on the line ++$error_count while ($line =~ /($error)/g); } }

  8. Use perltidy to format your code. It will be much easier to read.

I leave final assembly to the reader ;-).

Just noticed I had clicked on create rather than preview while composing this. Hope I didn't cause too much distress to our regular viewers.

Replies are listed 'Best First'.
Re^4: search log file with multiple word and count number of success
by steadybompipi (Novice) on Feb 24, 2008 at 13:59 UTC

    Thanks for the help...

    i have learnt more things after looking at it..

    i guess there is some misunderstanding under point 7. Actually i would like my script to count the number of time an error occur for today. The count should be by individual error. Think a example will be good. Please have a look at the below

    let say today date is 24th Feb and in /var/abcLogs:

    23 Feb error1 found. please check => this is not what i want as the date is today

    23 Feb error1 found. please check => this is not what i want as the date is today

    24 Feb error1 found. please check => this what i want as the line has today date and either errror1, 2 or 3

    24 Feb error1 found. please check => this what i want as the line has today date and either errror1, 2 or 3

    24 Feb error3 found. please check => this what i want as the line has today date and either errror1, 2 or 3

    24 Feb error2 found. please check => this what i want as the line has today date and either errror1, 2 or 3

    hence, the output result will show me:

    error1 found 2 for today

    error2 found 1 for today

    error3 found 1 for today

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-20 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found