我有一个文件,有一些条目,如
--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)
你的头衔不清楚.无论如何...
while(<>) {
next if !/^--ERROR/;
/Name="([^"]+)"\s+Age="([^"]+)"/;
print $1, " ", $2, "\n";
}
Run Code Online (Sandbox Code Playgroud)
可以从stdin读取; 当然,您可以将读取循环更改为其他任何内容,并根据您的需要使用某些内容填充哈希或其他内容.
作为一个班轮,尝试:
perl -ne 'print "$1 $2\n" if /^--ERROR/ && /Name="(.*?)"\s+Age="(.*?)"/;'
Run Code Online (Sandbox Code Playgroud)
这很像使用sed,但使用Perl语法.
| 归档时间: |
|
| 查看次数: |
10088 次 |
| 最近记录: |