[英]How can I change from a sed to Perl statement in command line?
I need to search a phrase in multi files and display the results on the screen. 我需要在多个文件中搜索短语并将结果显示在屏幕上。
grep "EMS" SCALE_*
| sed -e "s/SCALE_//" -e
"s/_main_.*log:/ /"
Since I am not get familiar with sed, I change it to Perl for convenient use. 由于我不熟悉sed,因此将其更改为Perl以方便使用。
grep "EMS" SCALE_*
| perl -e "s/SCALE_//" -e
"s/_main_.*log:/ /"
or 要么
grep "EMS" SCALE_*
| perl -e "s/SCALE_//; s/_main_.*log:/ /"
However , the last one is compiled but returns nothing on the command line. 但是,最后一个已编译,但在命令行上未返回任何内容。 Any suggestion for modifying my code. 关于修改我的代码的任何建议。 Great thanks! 万分谢意!
To use perl
in the style of sed
you should try the -p
flag: 要以sed
样式使用perl
,应尝试使用-p
标志:
perl -p -e "s/SCALE_//;" -e "s/_main_.*log:/ /;"
From the explanation in perlrun
: 从perlrun
的解释中:
-p -p
causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed: 使Perl在您的程序周围假设以下循环,从而使其遍历文件名参数(类似于sed):
LINE: while (<>) { ... # your program goes here } continue { print or die "-p destination: $!\\n"; }
You're not looping over the input lines with perl. 您不会在perl上循环输入行。 Check out -p or -n in the perlrun man page. 在perlrun手册页中检出-p或-n。
if you want to use Perl, then try this entirely in Perl 如果要使用Perl,请在Perl中完全尝试一下
while ( <> ){
chomp;
if ( $_ =~ /EMS/ ){
s/SCALE_//g;
s/main.*log://g;
print $_."\n";
}
}
on the command line 在命令行上
$ perl perl.pl SCALE_*
or this 或这个
$ perl -ne 'if (/EMS/){ s/SCALE_//g;s/main.*log://g; print $_} ;' SCALE_*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.