Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by Sandy (Deacon)
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 drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2014-09-22 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (208 votes), past polls