我如何像sed一样使用perl?

Raj*_*Raj 4 perl

我有一个文件,有一些条目,如

--ERROR--- Failed to execute the command with employee Name="shayam" Age="34"

--Successfully executed the command with employee Name="ram" Age="55"

--ERROR--- Failed to execute the command with employee Name="sam" Age="23"

--ERROR--- Failed to execute the command with employee Name="yam" Age="3"
Run Code Online (Sandbox Code Playgroud)

我只需要提取命令执行失败的名称和年龄.在这种情况下,我需要提取shayam 34 sam 23 yam 3.我需要在perl中执行此操作.非常感谢..

Eug*_*ash 23

作为单线:

perl -lne '/^--ERROR---.*Name="(.*?)" Age="(.*?)"/ && print "$1 $2"' file
Run Code Online (Sandbox Code Playgroud)


Nig*_*nns 20

perl -p -e's /../../ g'文件

或内联替换:

perl -pi -e's /../../ g'文件


Shi*_*zou 6

你的头衔不清楚.无论如何...

while(<>) {
 next if !/^--ERROR/;
 /Name="([^"]+)"\s+Age="([^"]+)"/;
 print $1, "  ", $2, "\n";
}
Run Code Online (Sandbox Code Playgroud)

可以从stdin读取; 当然,您可以将读取循环更改为其他任何内容,并根据您的需要使用某些内容填充哈希或其他内容.

  • 这在什么意义上像 sed 一样使用 perl?这是像 _perl_ 一样使用 perl。我更喜欢下面不同答案中的调用。 (4认同)

dao*_*oad 5

作为一个班轮,尝试:

perl -ne 'print "$1 $2\n" if /^--ERROR/ && /Name="(.*?)"\s+Age="(.*?)"/;'
Run Code Online (Sandbox Code Playgroud)

这很像使用sed,但使用Perl语法.