Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using unix grep command in Perl 4

Status
Not open for further replies.

zhenning

Technical User
Sep 22, 2005
50
CA
Hi Group,

I am new to Perl programming. I want to use unix grep command in perl program to grep a pattern in a file, and put the result line into an array, like @array = (/usr/bin/grep XXX abc.txt). How could I achieve this?

Thanks a lot!
zhenning
 
Something like this maybe?
Code:
#!/usr/bin/perl -w
use strict;
my $pattern = shift;
my @array   = ();
while(<>) {
  chomp;
  push @array, $_ if(/$pattern/);
}


Trojan.
 
To answer your specific question, you can do this:
Code:
my @array = qx/grep XXX abc.txt/;
However, shelling out like that is non-portable. Since this isn't a tricky thing to do in Perl, you'd be better off following Trojan's example and sticking to Perl in your script.
 
ishnid's code is very easy for me and got what I want. I will try to understand Trojan's code then. Thanks for everybody!!!
 
Most of the standard low-level unix command-line utilities have equivalents in perl. Staples like chown, chmod, link, mkdir etc all have their perl counterparts. Using them rather than the unix equivalents saves one process per invocation (often two if the shell is invoked to parse the arguments) and can make your code easier to read, more stable and much safer (it's quite easy to trick the shell into doing evil things by injecting shell metacharacters in unexpected places. You can (and should) code defensively but it is a good move to completely remove the shell from the equation by using the perl primitives instead).

Having ranted all that, grep is deceptive as the perl grep is barely related to the unix grep. The functionality of grep is, however, so closely integrated into perl itself (via the =~ and <> operators) that this is never a problem after you've met it the first time.

Interestingly, grep reimplemented in perl often outperforms it's standard equivalent.

Another trip for first-timers is cd. You can use the external cd but it won't do what you want. You must use chdir() instead.

My advice would be to get your code working any way you can and then go through it again, replacing system invocations of unix binaries with perl equivalents one by one. You'll see your code getting cleaner as you go and, for non-trivial applications, will also notice improved performance.

TMTOWTDI means you don't have to have a "big bang" when moving from, say, bash scripting to perl. You can ease yourself in at a rate at which you feel comfortable.

Yours,

f

[&quot;]As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.[&quot;]
--Maur
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top