How can I implement grep
of Unix in Perl? I tried to use Perl's built-in grep
. Here is the code which is not working:
$pattern = @ARGV[0];
$file= @ARGV[1];
open($fp,$file);
@arr = <$fp>;
@lines = grep $pattern, @arr;
close($fp);
print @lines;
And by the way, i am trying only basic grep
functionality not full featured and secondly i don't want to do string parsing myself. I want to use inbuilt grep
or some function of Perl.
Thanks in advance :)
The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.
in Perl. If you have an existing awk program, and wish it to run with Perl, you can perform a mechanical translation using the a2p utility provided with the Perl distribution. This utility converts the awk syntax into the Perl syntax, and for the vast majority of awk programs, provides a directly runnable Perl script.
As you already accepted an answer, I am writing this answer for reference for future readers searching for similar problems, but not exactly yours:
As people have answered already, the way of simulating grep with perl is to use the online approach. For the use of perl as a 'better' grep (and find and cut and...) I recomend the book minimal perl and you are lucky because the chapter for 'perl as a "better" grep' is one of the sample chapters.
Here you have more examples inspired from the book:
perl -wnle '/foo/ and print' null.txt # normal grep
perl -wnle '/foo/ and print "$ARGV: $_"' null.txt # grep -H
perl -wnle '/foo/ and print $ARGV and close ARGV' null_1.txt null_2.txt # grep -l
In the last example ARGV is the current filehandle, and as with -l you are interested in finding files with the match you can print the file name and go for the next file after the first match in a file.
Also you can search by paragraph instead by line:
$ perl -00 -wnl -e '/\bBRIBE\b/i and print;' SenQ.testimony
I knew I'd be in trouble if
I ACCEPTED THE BRIBE!
So I did not.
My minimum bribe is $100k, and she only offered me $50k,
so to preserve my pricing power, I refused it.
Or find only the first match:
$ perl -00 -wnl -e '/\bBRIBE\b/i and close ARGV;' SenQ.testimony
I knew I would be in trouble if
I ACCEPTED THE BRIBE!
So I did not.
And finally if you ask about grep and perl, I think thay I should mention ACK. It implements, in perl, the grep functionality and extend it. This is a wonderful tool and as a plus you can have it also as a CPAN package. I have always use as a command line, I don't know if you can access its methods directly from your perl programs but this would be very nice.
In Perl to refer an entire array we use @
. But to refer the individual elements, which are scalar we use $
.
So, you need to use $
and not @
on these lines:
$pattern = @ARGV[0];
$file= @ARGV[1];
Also
this
@lines = grep $pattern, @arr;
should be
@lines = grep /$pattern/, @arr;
the grep in Perl has the general syntax of:
grep EXPR,LIST
It evaluates the EXPR
for each element of LIST
and returns the list value consisting of those elements for which the expression evaluated to true.
The EXPR
in your case is searching for the pattern $pattern
in array @arr
. To search you need to use the /PATTERN/
without the /
the string $pattern
will be evaluated for true or false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With