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

  • 本文关键字:perl 管道 命令行 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 -->

如您所见,管道输入只是被回显。如何预防这种情况?

尝试

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
}

最新更新