What is the equivalent of “grep -n” in Perl?
I want to grep a word with line number. It's easily possible in the shell with the command grep -n
or with sed
. Is there any equivalent available in Perl? I have checked the grep fu开发者_如何学Pythonnction, however I am unable to find anything like I need.
In a file called mygrep:
#!/usr/bin/perl
use strict;
use warnings;
my $match = shift;
while (<>) {
if (/\Q$match/) {
print "$. : $_";
}
}
Then from the command line:
$ ./mygrep if mygrep
6 : my $match = shift;
9 : if (/\Q$match/) {
Should be enough to get you started.
精彩评论