in reply to Write a file if grep finds a pattern?

Consider this
$ perl -MDDS -le" Dump($_); print grep /stuff/, 1,2,3,stuff; Dump($_)" $VAR1 = undef; stuff $VAR1 = undef; $ perl -MDDS -le" Dump($_); warn print grep /stuff/, 1,2,3,stuff; Dump +($_)" $VAR1 = undef; stuff 1 at -e line 1. $VAR1 = undef;
You want something like
my $returncode; while(<IN>){ if( /error/i ){ print; $returncode++; } } if ( $returncode ){ BoogieWoogie(); }

Replies are listed 'Best First'.
Re^2: Write a file if grep finds a pattern?
by matze77 (Friar) on Nov 19, 2010 at 09:37 UTC
    Thanks, that did the trick.
    use strict; use warnings; my $returncode; $returncode = 0; open (my $IN,"<c:/temp/logbook.log") || die "can not open datei: $!"; while(<$IN>){ if( /error/i ){ print; $returncode++; print "$returncode\n"; } } if ( $returncode == 0 ){ my $file = '/temp/ok.txt'; open FILE, '>', $file or die "Kann Datei $file nicht zum Schreiben +oeffnen: $!\n"; print FILE "ok!\n"; close FILE; } close $IN;


    Thanks
    MH