in reply to Write a file if grep finds a pattern?
use strict; use warnings; open (my $in,"<", "/tmp/logbook.log") or die "can not open datei: $!"; my @errors = grep /error/i, <$in>; my $file = '/tmp/ok.txt'; open my $out, ">", $file or die "Kann Datei $file nicht zum Schreiben +oeffnen: $!\n"; if (@errors) { print $out @errors; } else { print $out "OK!\n"; } close($in); close($out);
line1 has an error line2 does not ERROR on line 3the following output is in the ok.txt file:
line1 has an error ERROR on line 3
|
|---|