我知道如何使用sed带grep,但在Perl中下面的失败.如何sed在Perl程序中工作?
chomp (my @lineNumbers=`grep -n "textToFind" $fileToProcess | sed -n 's/^\([0-9]*\)[:].*/\1/p'`)
Run Code Online (Sandbox Code Playgroud)
Jas*_*hen 25
建议:使用Perl正则表达式和替换而不是grep或sed.
它的语法基本相同,但功能更强大.最后,它比调用额外的sed进程更有效.
Pau*_*lin 14
你需要用grep或sed做的任何事情都可以更容易地在perl中本地完成.例如(这大致是正确的,但可能是错误的):
my @linenumbers;
open FH "<$fileToProcess";
while (<FH>)
{
next if (!m/textToFind/);
chomp;
s/^\([0-9]*\)[:].*/\1/;
push @lineNumbers, $_;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
46734 次 |
| 最近记录: |