Jon W wrote:
Hi.
I have a log file that contains a collection of individual test
results, each beginning with a "START", and ending with a "FINISH".
I would like to print out the names of the tests that have the error
message "LOOKING FOR THIS STRING".
log file:
----------------------------------------------------
START : c:\disk1\test11.exe
12:50:09 AM Warning: Value of changed
12:50:09 AM Warning: Value of updated
12:50:09 AM Warning: Parameter ignored
TEST_PASSED
Time - Total,Sim,Load,Compare,Save = 13850,13761,59,10,20 mS
FINISH
START : c:\disk1\test22.exe
12:50:09 AM Warning: Value of changed
12:50:09 AM Error: LOOKING FOR THIS STRING
12:50:09 AM Warning: Parameter ignored
12:52:22 AM Error: LOOKING FOR THIS STRING
TEST_FAILED
Time - Total,Sim,Load,Compare,Save = 13850,13761,59,10,20 mS
FINISHED
START : c:\disk1\test33.exe
.
.
.
----------------------------------------------------
All that I can figure out how to do is to print out the line with the
test name, and also the desired "LOOKING FOR THIS STRING" line with
the following script:
-----------------------------------------------------------------------------------------
#!/usr/bin/perl -w
use strict;
use warnings;
my $logfile = 'test.log';
my @list;
my @list2;
my $line2 = '';
open(LOG, $logfile) or die "Couldn't open $logfile: $!\n";
# loop through each line, assigning $line as we go
while( my $line = <LOG> ){
# test for the pattern
if( $line =~ /(.*)\.exe/ || $line =~ /LOOKING FOR THIS STRING/ ){
# $1 contains the first captured string
push(@list, $line);
}
}
close(LOG);
# print out all the captured numbers
map{ print "$_\n"; } @list;
-----------------------------------------------------------------------------------------
For example, this outputs:
START : c:\disk1\test11.exe
START : c:\disk1\test22.exe
12:50:09 AM Error: LOOKING FOR THIS STRING
START : c:\disk1\test33.exe
Does anybody have any ideas of how I should be trying to approach this
program to get what I actually need? For the above output, all I
really wanted is to print out "c:\disk1\test22.exe", as that test had
the particular failure output.
How about the program below. Hope it helps.
Rob
use strict;
use warnings;
my $logfile = 'test.log';
open LOG, $logfile or die "Couldn't open $logfile: $!";
my @test;
my $error;
while (<LOG>) {
if (/^START/ .. /^FINISH/) {
push @test, $_ ;
$error = 1 if /LOOKING FOR THIS STRING/;
}
if (/^FINISH/) {
if ($error) {
print @test;
undef $error;
}
undef @test;
}
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/