in reply to Perl/Unix question; returning grep as a boolean and returning a match
Hi,
I am not sure if I understand you correctly, but if I do, then this should do what you want
You should note that using backticks (rather than the system command) will return the result of the system command (what is normally written to the terminal) rather than the exit code.#!/usr/bin/perl use strict; use warnings; my $y = 'Eclipse'; my $p = 'myfile.gz'; # x will contain the line(s) that contain y my $x = `gzgrep $y $p`; unless ( $x ){ print "could not find exact case, but...\n"; $x = `gzgrep -i $y $p`; } if ($x) { print "found:\n$x\n"; } else { print "nothing found\n"; }
The exception to this is error messages. Unless you specifically redirect stderr to stdin, stderr will still be displayed on the screen, and will not be captured by $x
Only one gzgrep!
#!/usr/bin/perl use strict; use warnings; my $y = 'Eclipse'; my $p = 'myfile.gz'; my @x; my $z; @x = grep /$y/, $z= `gzgrep -i $y $p`; if ( @x ){ print "found @x\n"; } else { print "could not find exact case, but...\n"; if ($z) { print "found:\n$z\n"; } else { print "found nothing\n"; } }
|
|---|