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

I am reading the below log file and looking for the word error .If it finds error then it should print some stuff. But it is not printing when there is error.



LOG FILE:
QA-METS - Checksum log UID=61110530R Title=Lecture introductory to the course on anatomy, in the University + of Pennsylvania / FileID=BSE1 Filename=61110530R_000001.jpg METS: Mimetype=image/jpg Size=1950329 MD5=fb0ff0b498cb6ce79df20fdddd71 +da3f [Error] Checksum mismatch; Computed MD5=08141ce3c84cb54d57d40b502331 +2c53 FileID=BSE2 Filename=61110530R_000002.jpg METS: Mimetype=image/jpg Size=737596 MD5=7b44c390f7279a3f266652a96a5f6 +86d [Error] Checksum mismatch; Computed MD5=462db8d6b278f6fdb63f87fd2974 +e7e9 FileID=BSE3 Filename=61110530R_000003.jpg METS: Mimetype=image/jpg Size=1010181 MD5=8acd6e39a21f0fc18dfb6a54663f +a472 [Error] Checksum mismatch; Computed MD5=7e9c7959e3a3b60c4d2f80e089f0 +3f9d FileID=BSE4 Filename=61110530R_000004.jpg METS: Mimetype=image/jpg Size=655993 MD5=f655a572365bcea7c878201b7606d +68a [Error] Checksum mismatch; Computed MD5=0fbdfb7ba703e0ad810025135e4e +8070 FileID=BSE5 Filename=61110530R_000005.jpg METS: Mimetype=image/jpg Size=838116 MD5=2a055f3579db7f9c583c8cf84afdf +030 [Error] Checksum mismatch; Computed MD5=50a8ee3e882b79de5fe8a47be54c +c478 FileID=BSE6 Filename=61110530R_000006.jpg METS: Mimetype=image/jpg Size=511383 MD5=8dfa19092cdf0d76bf99eede88fae +e19 [Error] Checksum mismatch; Computed MD5=3380c9f79217ba0faee294ab9cff +db81 FileID=BSE7 Filename=61110530R_000007.jpg METS: Mimetype=image/jpg Size=881218 MD5=97d8a953cc1b8ab1dff728a8b0814 +6be [Error] Checksum mismatch; Computed MD5=37fa1505f9ca13251406658653af +4439 FileID=BSE8 Filename=61110530R_000008.jpg METS: Mimetype=image/jpg Size=496683 MD5=638a7f3cb7d2b189751c62c91e018 +d28 [Error] Checksum mismatch; Computed MD5=d008d5852cfaff6cf14fa0748b5d +044f

Current Script
#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; $/ = ""; my @InputFiles = (</usr/mlm/qa/logs/hhl/BATCH*/*/*_checksum.log>); foreach my $InputFile ( @InputFiles ) { print "$InputFile\n"; my %errors; open my $input_fh, '<', $InputFile or die "Can't open input file ($InputFile): $!"; my $uid = <$input_fh>; ($uid) = $uid =~ /UID=(\S+)/; while ( my $record = <$input_fh> ) { my ($md5) = $record =~ /METS:.+?MD5=(\S+)/; my ($filename) = $record =~ /Filename=(\S+)/; #my ($error) = $record =~ /[Error] +MD5=(\S+)/; my ($error) = $record =~ /\Q[Error]\E .+MD5=(\S+)/; if ( $error ) { $errors{$filename}{md5} = $md5; $errors{$filename}{computed} = $error; } } close $input_fh; my $count = keys %errors; foreach my $filename ( sort keys %errors ) { print join('|', $uid, $filename, $count, $errors{$filename}{md5}, $errors{$filename}{computed} ),"\n";

Replies are listed 'Best First'.
Re: LOG FILE HELP IN PERL
by toolic (Bishop) on Jan 19, 2011 at 17:08 UTC
    You didn't show us what you expect to be printed. But, when I fix the compile errors in the code you posted, then run it, I get the following output:
    ./foo.log Use of uninitialized value $uid in join or string |61110530R_000001.jpg|8|fb0ff0b498cb6ce79df20fdddd71da3f|08141ce3c84cb +54d57d40b5023312c53 Use of uninitialized value $uid in join or string |61110530R_000002.jpg|8|7b44c390f7279a3f266652a96a5f686d|462db8d6b278f +6fdb63f87fd2974e7e9 Use of uninitialized value $uid in join or string |61110530R_000003.jpg|8|8acd6e39a21f0fc18dfb6a54663fa472|7e9c7959e3a3b +60c4d2f80e089f03f9d Use of uninitialized value $uid in join or string |61110530R_000004.jpg|8|f655a572365bcea7c878201b7606d68a|0fbdfb7ba703e +0ad810025135e4e8070 Use of uninitialized value $uid in join or string |61110530R_000005.jpg|8|2a055f3579db7f9c583c8cf84afdf030|50a8ee3e882b7 +9de5fe8a47be54cc478 Use of uninitialized value $uid in join or string |61110530R_000006.jpg|8|8dfa19092cdf0d76bf99eede88faee19|3380c9f79217b +a0faee294ab9cffdb81 Use of uninitialized value $uid in join or string |61110530R_000007.jpg|8|97d8a953cc1b8ab1dff728a8b08146be|37fa1505f9ca1 +3251406658653af4439 Use of uninitialized value $uid in join or string |61110530R_000008.jpg|8|638a7f3cb7d2b189751c62c91e018d28|d008d5852cfaf +f6cf14fa0748b5d044f
    Update: At AM's request, here is a compilable version of the OP's code (2 extra curlies at the end):
    #!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; $/ = ""; my @InputFiles = (</usr/mlm/qa/logs/hhl/BATCH*/*/*_checksum.log>); foreach my $InputFile ( @InputFiles ) { print "$InputFile\n"; my %errors; open my $input_fh, '<', $InputFile or die "Can't open input file ($InputFile): $!"; my $uid = <$input_fh>; ($uid) = $uid =~ /UID=(\S+)/; while ( my $record = <$input_fh> ) { my ($md5) = $record =~ /METS:.+?MD5=(\S+)/; my ($filename) = $record =~ /Filename=(\S+)/; #my ($error) = $record =~ /[Error] +MD5=(\S+)/; my ($error) = $record =~ /\Q[Error]\E .+MD5=(\S+)/; if ( $error ) { $errors{$filename}{md5} = $md5; $errors{$filename}{computed} = $error; } } close $input_fh; my $count = keys %errors; foreach my $filename ( sort keys %errors ) { print join('|', $uid, $filename, $count, $errors{$filename}{md5}, $errors{$filename}{computed} ),"\n"; } }

      Not sure why the same above script is not printing the output for me.

      If possible can you post the code fixed code here.

Re: LOG FILE HELP IN PERL
by JavaFan (Canon) on Jan 19, 2011 at 17:49 UTC
    my $uid = <$input_fh>; ($uid) = $uid =~ /UID=(\S+)/;
    That tries to find a UID in the first paragraph of the file. However, the first paragraph is the line starting with QA-METS. You want to skip the first paragraph, and do this match against the second.
Re: LOG FILE HELP IN PERL
by tilly (Archbishop) on Jan 19, 2011 at 17:12 UTC
    Your bug is that you are setting $md5 on every single line of input. So when you hit an error line, you no longer know the md5 from the line before.

    To fix it you need to define $md5 outside of the while loop that reads through the file, and then inside the file you need to set it only if the record matched. That way it will get set when you encounter that line, and it will still be set on the error line.

    (Incidentally you left two closing braces off of your cut and paste of your script.)

    Edit: JavaFan is correct. I'd missed paragraph mode which does fix that potential problem. The cause of the undef warnings is that $UID is not populated, because the UID line is on the second paragraph, not the first. If you add another <$input_fh>; that warning goes away. (This is very, very fragile logic. But it seems to work.)

    If there is still no output, then I would advise checking whether it is looking for the files in the right place.

      Your bug is that you are setting $md5 on every single line of input. So when you hit an error line, you no longer know the md5 from the line before.
      Except that he has $/ = "";, which makes it read paragraph by paragraph.