简体   繁体   English

从perl文件中返回具有匹配模式的行

[英]return lines with matching pattern from a file in perl

I need to write a perl script to extract the lines matching different patterns and then operate onto it. 我需要编写一个perl脚本来提取匹配不同模式的行,然后对其进行操作。 I've done following to match and extracts all the lines which contain the matching pattern: 我已经完成以下匹配并提取包含匹配模式的所有行:

my $filetoread = <$curr_dir/p*.out>;
my $filetoreadStrings=`strings $filetoread | egrep \"(Preparing extraction | 100.00%)\"`;
my @ftr = split('\n', $filetoreadStrings);
chomp (@ftr);

my $FirstPattern = (grep /Preparing extraction/, @ftr)[0];
my $SecondPattern = (grep /100.00% done/, @ftr)[0];
print "$FirstPattern \n";
print "$SecondPattern \n";

I'm getting lines with the second pattern only, even though the first pattern (Preparing extraction) is present in the log. 即使日志中存在第一个模式(准备提取),我也只能使用第二个模式的行。 I think the problem is with or '|'. 我认为问题出在或'|'。 I need to know how to get all the lines matching the patterns. 我需要知道如何使所有与模式匹配的行。 [$curr_dir is the directory I'm running the script from and P*.out is the log file] [$ curr_dir是我正在运行脚本的目录,P * .out是日志文件]

`... egrep \"(Preparing extraction | 100.00%)\" ...`

should be 应该

`... egrep \"(Preparing extraction| 100.00%)\" ...`

or just 要不就

`... egrep "(Preparing extraction| 100.00%)" ...`

my $filetoread = <$curr_dir/p*.out>;

should be 应该

my ($filetoread) = <$curr_dir/p*.out>;

Don't call <glob_pattern> in scalar context unless you call it until it returns undef. 不要在标量上下文中调用<glob_pattern> ,除非您调用它直到返回undef。


my @ftr = split '\n', `...`;
chomp(@ftr);

should be 应该

my @ftr = split /\n/, `...`;

or 要么

chomp( my @ftr = `...` );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM