Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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"; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://908045]
help
Chatterbox?
and !@monks...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2017-11-22 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (316 votes). Check out past polls.

    Notices?