如何在命令行中从sed语句更改为Perl语句?

时间:2010-01-27 08:25:48

标签: perl sed grep

我需要在多个文件中搜索一个短语并在屏幕上显示结果。

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:/ /"  

但是,最后一个是编译的,但在命令行中没有返回任何内容。 任何修改我的代码的建议。 十分感谢!

3 个答案:

答案 0 :(得分:8)

要以perl的方式使用sed,您应该尝试-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";
  }
  

答案 1 :(得分:3)

你没有用perl循环输入行。在perlrun手册页中查看-p或-n。

答案 2 :(得分: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_*
相关问题