2013-10-11 39 views
1

在我的命令提示符下,我運行了一個grep並得到了以下結果。如何禁止在命令行上發送給perl的管道輸入?

$ 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 --> 

正如你所看到的,管道輸入簡單地得到了呼應。如何防止這一點?

回答

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 
} 
+0

在第一個命令中,perl不打印任何東西,但我們看到打印的子字符串。爲什麼? – CodeBlue

+1

@CodeBlue檢查更新 –