open (DIFFR, "/tmp/log.diff"); while () { $line=$_; chomp($line); if ($line =~ /ERROR/) { print CSVFILE "$oldline\n"; #look below if stat. print CSVFILE "$line,"; } $oldline = $line; #sets a variable with the last line }