2010-01-27 71 views
0

我需要搜索多個文件中的短語並將結果顯示在屏幕上。如何在命令行中從sed更改爲Perl語句?

grep "EMS" SCALE_* 
| sed -e "s/SCALE_//" -e 
"s/_main_.*log:/ /" 

由於我不熟悉sed,爲了方便使用,我將其更改爲Perl。

grep "EMS" SCALE_* 
| perl -e "s/SCALE_//" -e 
"s/_main_.*log:/ /" 

grep "EMS" SCALE_* 
| perl -e "s/SCALE_//; s/_main_.*log:/ /" 

然而,最後一個被編譯,但返回的命令行上什麼都沒有。 任何修改我的代碼的建議。 非常感謝!

+0

我建議你學習SED,或重新振作,或兩者兼而有之。非常方便的語言。 – 2010-01-27 08:43:15

回答

2

,如果你想使用Perl,那就試試這個完全在Perl

while (<>){ 
    chomp; 
    if ($_ =~ /EMS/){ 
     s/SCALE_//g; 
     s/main.*log://g; 
     print $_."\n"; 
    } 
} 

命令行

$ perl perl.pl SCALE_* 

或本

$ perl -ne 'if (/EMS/){ s/SCALE_//g;s/main.*log://g; print $_} ;' SCALE_* 
3

你不是遍歷上輸入行與perl。在perlrun手冊頁查看-p或-n。

8

要在sed風格使用perl你應該嘗試-p標誌:

perl -p -e "s/SCALE_//;" -e "s/_main_.*log:/ /;" 

從解釋中perlrun

-p

導致的Perl假設您的程序周圍有以下循環米,這使得它遍歷文件名參數有點像sed:

LINE: 
    while (<>) { 
    ...    # your program goes here 
    } continue { 
    print or die "-p destination: $!\n"; 
    }