简体   繁体   English

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

[英]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.

相关问题 如何在Perl中从命令行读取两个文件? - How can I read two files from command line in Perl? 如何在awk或perl(或python,或......)中编写这个sed / bash命令? - How can I write this sed/bash command in awk or perl (or python, or …)? sed命令在命令行上运行,但不在perl脚本中 - sed command working on command line but not in perl script 如何强制 perl 仅从 stdin 而不是从命令行上的文件处理 args? - How can I force perl to process args ONLY from stdin and not from a file on command line? 我可以从Windows命令行使用哪个perl? - Which perl can I use from the Windows command line? 如何从Perl中的输入检查命令行参数是否等于“ &lt;”? - How can I check if a command-line argument is equal to '<' from input in Perl? 如何让Perl脚本接受来自STDIN和命令行参数的参数? - How can I get a Perl script to accept parameters from both STDIN and command line arguments? 如何使用命令行参数从bash运行perl脚本? - How can I run a perl script from bash using command line arguments? 如何使用 Perl 从 Windows 命令行获取文件的 SHA1 哈希? - How can I use Perl to get a SHA1 hash of a file from the Windows command line? 如何从命令行检查我的系统上是否安装了Perl模块? - How can I check if a Perl module is installed on my system from the command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM