Re: Write a file if grep finds a pattern?
by chrestomanci (Priest) on Nov 19, 2010 at 09:37 UTC
|
One tip I can offer is to avoid using the default variable ($_) in scripts unless you know what you are doing. There are lots of case where it does not do what you expect, or has unexpected side effects. It is much better to use explicit named variables in scripts. IMHO, frequent use of the default variable makes outsiders think that perl is a write only language.
On the other hand, if you are trying to write a perl one liner with perl -e or demonstrate your skills in an obfuscated perl contest then go for it, but in those cases, readability and robustness are less important
Also, you appear to be mixing up file handles as bare words and as scalars (IN vs $IN).
Back to your problem, You wrote if ($_ ==0). I think that on that line $_ will contain the last line read from the file, so the if statement will not do what you expect. (I could be wrong though, as I also get confused about how the default variable works in some cases.) Personally I would write something like:
use strict;
use warnings;
open (my $IN, '<', 'c:/temp/logbook.log') or die "can not open datei:
+$!";
my $error_count = 0;
while( my $line = <$IN> )
{
if( $line =~ m/error/i )
{
print $line;
$error_count ++;
}
}
close $IN;
if( 0 == $error_count )
{
open FILE, '>', $file or die "Kann Datei $file nicht zum Schreiben
+oeffnen: $!\n";
print FILE "ok!\n";
close FILE;
}
| [reply] [d/l] [select] |
Re: Write a file if grep finds a pattern?
by mjscott2702 (Pilgrim) on Nov 19, 2010 at 09:37 UTC
|
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
| [reply] [d/l] |
Re: Write a file if grep finds a pattern?
by rovf (Priest) on Nov 19, 2010 at 08:58 UTC
|
| [reply] [d/l] [select] |
Re: Write a file if grep finds a pattern?
by Anonymous Monk on Nov 19, 2010 at 09:04 UTC
|
$ 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();
}
| [reply] [d/l] [select] |
|
|
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
| [reply] [d/l] |
Re: Write a file if grep finds a pattern?
by JavaFan (Canon) on Nov 19, 2010 at 11:25 UTC
|
Can't resist giving a shell solution, as it's shorter than the Perl solutions offered so far:
if grep -l error /temp/logbook.log > /dev/null
then echo 'ok!' > /temp/ok.txt
fi
| [reply] [d/l] |
|
|
if( `grep -l error /temp/logbook.log > /dev/null`)
`echo 'ok!' > /temp/ok.txt`
}
| [reply] [d/l] |
|
|
grep error c:/temp/logbook.log&&echo 'ok!'>/temp/ok.txt
Slapping backquotes on it to make it "Perl", still is two characters more. | [reply] [d/l] |
Re: Write a file if grep finds a pattern?
by CountZero (Bishop) on Nov 21, 2010 at 10:24 UTC
|
{
open my $IN, '<', 'c:/temp/logbook.log' or die "cannot open datei:
+ $!";
open my $FILE, '>', 'c:/temp/ok.txt' or die "Kann Datei 'c:/temp/o
+k.txt' nicht zum Schreiben oeffnen: $!";
say $FILE 'ok!' if scalar grep /error/, <$IN>;
}
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [d/l] |