如何在命令行上抑制发送到perl的管道输入?

时间:2013-10-11 19:53:43

标签: perl pipe

在我的命令提示下,我跑了一个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 -->

正如您所看到的,管道输入简单地得到了回应。怎么预防这个?

1 个答案:

答案 0 :(得分:2)

尝试

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

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

说明:-p开关会自动打印当前行($_)而-n切换不会。

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
}