steadybompipi has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on search log file with multiple word and count number of success

Replies are listed 'Best First'.
Re: search log file with multiple word and count number of success
by roboticus (Chancellor) on Feb 22, 2008 at 12:15 UTC
    steadybompipi:

    You can use <p> to put paragraph codes in your messages. In fact, after you first preview your message, there's a list of useful tags you can use listed below the textbox. (Hmmmm.... it perhaps ought to be on the initial comment page as well...)

    ...roboticus

      it perhaps ought to be on the initial comment page as well...

      Good idea. Done! Thanks for the suggestion.

      A word spoken in Mind will reach its own level, in the objective world, by its own weight
        jdporter:

        ++ Thanks! Now I can quit opening an extra (tab/page)1 to find the Perl Monks Approved HTML tags. (For some reason, I just can't seem to remember them....)

        Of course you realize that you just made a huge mistake by implementing that so quickly. Now I just might come up with more work for you to do! ;^)

        Thanks again ... that really helps. (I wish I had thought to mention it sooner!)

        ...roboticus

      Hi all! I need same script but I am a newbie in perl. Could you help me pls? :) Regards, Ioan
Re: search log file with multiple word and count number of success
by jdporter (Paladin) on Feb 22, 2008 at 12:11 UTC

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

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