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

What is the relevance of the title of your post? I don't see any, so I'll ignore it (for now).

If you have questions about why the site behaves the way it does, you could try reading the site FAQ, which is linked at the top of every page as Need Help??.

To understand a little about how writeups are formatted, you should have read the hints displayed below the 'Preview' button of the post form. The short answer is that we use the content formatting language of the Web, called HTML (with some very minor variations). You might want to learn it.

A word spoken in Mind will reach its own level, in the objective world, by its own weight
  • Comment on Re: search log file with multiple word and count number of success

Replies are listed 'Best First'.
Re^2: search log file with multiple word and count number of success
by steadybompipi (Novice) on Feb 23, 2008 at 08:38 UTC
    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?

      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.

      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

      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/)