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

sorry, not sure why is my previous post gone. Pls have alook at the below post:

Trying to write a script that can search for a log file which pass the two requirement and generate number of count of specific error

2 requirement - each line must be certain date, and either error1 or error2 or error3 or error4 Tried something below

my $time = localtime(); my ($wd,$mon,$mond,$times,$year) = split /\s+/, $time; my $pattern_date = "$mon $mond"; my @array = ("error4", "error3", "error2", "error1"); my $count; my @word_search; my $line; my $w; my $wordcount; open (OUTFILE, "/home/me/textfile.txt") || die ("can not open file for + writing"); while ($line = <OUTFILE>) { chop $line; @word_search = split(/ /,$line); $w = 1; while ($w <= @word_search) { if ($word_search[$w-1] eq $pattern_date && $word_search[$w-1] eq $arra +y[$count]) { $wordcount++; } $w++; } } #print ("$array[$count] occurs $wordcount\n"); #print "$pattern_date found $wordcount\n"; #} close (OUTFILE);
And i am struck here. not sure if the split that i use really work and my
"if ($word_search[$w-1] eq $pattern_date && $word_search[$w-1] eq $arr +ay[$count]) { "
is correct. Anyone help?

Replies are listed 'Best First'.
Re^3: search log file with multiple word and count number of success
by jdporter (Paladin) on Feb 23, 2008 at 13:45 UTC

    You'd be more likely to get help here if you play by our rules. Fortunately, they're not hard. Please read How do I post a question effectively?. You were already admonished about this. It's not too late to reformat your post.

Re^3: search log file with multiple word and count number of success
by hipowls (Curate) on Feb 24, 2008 at 04:21 UTC

    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.

      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

Re^3: search log file with multiple word and count number of success
by steadybompipi (Novice) on Mar 02, 2008 at 13:49 UTC

    found a way to solve my problem. Surprisely easy syntax

    when i found out the answer but was thinking might help

    some guy who is new to perl just like me. =)

    Answer as below -

    if ( $line =~/$month $day $year/ && $line =~ /error1/)