开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜