[Sderby] PERL again!

Andrew White sderby at mailman.lug.org.uk
Thu Jan 9 17:27:02 2003


> I'm currently writing a PERL script to analyse log files from Legarto backup
> server.  What I want to do is find the failures, which are idenified by a
> line with "Unsuccessful save sets" then a blankish line, then the fialures
> then another blank line.  What I'm thinking of doing if none has a better
> idea is searching for "unsuccessful save sets" finding the line number and
> the adding one and extracting a line at a time for analysis until a blank
> line, what I need to find out how to do is how to find the line number of the
> line and how to extract lines given their number.

I wrote a quick hack to do this, not exactly great, but it works:

given the data is in the file test:

$prob=0;
open(A,"test");
while (<A>) {
if ($prob==2 && /^$/) {$prob=0;next;}
if ($prob==2) {print "$.\n";}
if ($prob==1 && /^$/) {$prob=2;next;}
if (/unsuccessful save sets/) {$prob=1;}
}

will dump the line numbers with problems. You could actually just extract
the lines themselves, if you change "$." to "$_", which would make more
sense to me. If you want to grab the lines later, use the script as is,
and you can recurse through a similar while-loop, printing the incrementing
a counter and writing out the lines in question as you go along. There's
probably a better way of doing this, but I can't think of one right now.
My brain's going.