1

在我的命令提示符下,我运行了一个 grep 并得到了以下结果。

$ grep -r "javascript node" 

restexample/NewsSearchService/V1/madonna_html.html:<!-- start empty javascript node for popup app fix -->
restexample/NewsSearchService/V1/madonna_html.html:<!-- end empty javascript node for popup app fix -->

现在,假设我想删除 " restexample" 部分。我可以通过使用

print substr($_,13)

但是,当我通过管道传输到 perl 时,这就是我得到的 -

grep -r "javascript node" | perl -pe ' print substr($_,11) ' 
/NewsSearchService/V1/madonna_html.html:<!-- start empty javascript node for popup app fix -->
restexample/NewsSearchService/V1/madonna_html.html:<!-- start empty javascript node for popup app fix -->
/NewsSearchService/V1/madonna_html.html:<!-- end empty javascript node for popup app fix -->
restexample/NewsSearchService/V1/madonna_html.html:<!-- end empty javascript node for popup app fix -->

如您所见,管道输入只是得到了回显。如何防止这种情况?

4

1 回答 1

2

尝试

grep -r "javascript node" | perl -lpe '$_ = substr($_,11)'

或者

grep -r "javascript node" | perl -lne 'print substr($_,11)'

说明:-pswitch 自动打印当前行 ( $_) 而-nswitch 不会。

perl -MO=Deparse -lpe '$_ = substr($_,11)'
BEGIN { $/ = "\n"; $\ = "\n"; }
LINE: while (defined($_ = <ARGV>)) {
    chomp $_;
    $_ = substr($_, 11);
}
continue {
    die "-p destination: $!\n" unless print $_; # <<< automatic print
}
于 2013-10-11T19:54:50.440 回答