在我的命令提示下,我跑了一个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 -->
正如您所看到的,管道输入简单地得到了回应。怎么预防这个?
答案 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
}