Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Write a file if grep finds a pattern?

by mjscott2702 (Pilgrim)
on Nov 19, 2010 at 09:37 UTC ( #872455=note: print w/ replies, xml ) Need Help??


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

You could try something like this:

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);


If the logbook.log file has something like this:
line1 has an error
line2 does not
ERROR on line 3
the following output is in the ok.txt file:

line1 has an error
ERROR on line 3


Comment on Re: Write a file if grep finds a pattern?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://872455]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2014-07-23 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (153 votes), past polls