Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Perl/Unix question; returning grep as a boolean and returning a match

by Sandy (Curate)
on Jun 03, 2011 at 19:22 UTC ( #908045=note: print w/ replies, xml ) Need Help??


in reply to Perl/Unix question; returning grep as a boolean and returning a match

UPDATE: New code that only uses ONE gzgrep

Hi,

I am not sure if I understand you correctly, but if I do, then this should do what you want

#!/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"; }
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.

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"; } }


Comment on Re: Perl/Unix question; returning grep as a boolean and returning a match
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2014-12-20 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls