in reply to need to extract top and bottom lines...

How about
my $previous; open F, "< /tmp/log.diff" or die "Unable to open file : $!\n"; while (<F>) { chomp; if (m/ERROR/) { print CSVFILE, "$previous\n$_\n"; } $previous = $_; }

--
zejames