示例文件:
some texts....
....
FINAL RESULTS
....
some texts
Needed data: -25.684 -61.647 -20.061 #need this line
-61.647 82.482 -15.683 #need this line
-20.061 -15.683 -56.799 #DOES NOT need this line
Needed data: -24.557 -48.468 2.592 #need this line
-48.468 62.053 -6.742 #need this line
-2.592 -6.742 -37.495 #DOES NOT need this line
Needed data: -42.875 -101.901 -30.377 #need this line
-101.901 134.323 -24.259 #need this line
-30.377 -24.259 -91.448 #DOES NOT need this line
我想搜索“FINAL RESULTS”,然后搜索“Needed data”并提取第一行和第二行但不第三行的数字,并将它们放入数组中。
这是我到目前为止所拥有的:
my $in_sec;
open(IN, "< t.in") or die;
while (<IN>)
{
if (/FINAL RESULTS/) { $in_sec = 1; }
elsif (/(?<!FINAL RESULTS):/) { $in_sec = 0; }
if ($in_sec) {
if(/Needed data/) {
@a = split;
print "@a\n";
if ($_ = <IN>) {
@b = split;
print "@b\n";
}
}
}
}
但是没有打印任何内容...
感谢您的帮助! 杰夫
用 Perl 的一句话:
perl -0nE '
push @a, split for /(?<=Needed data:)\s+\S+\s+\S+\s+\S+\N\s*\S+\s+\S+\s+\S+/g;
END{say join "\n", @a}
' t.in
-25.684
-61.647
-20.061
-61.647
82.482
-15.683
-24.557
-48.468
2.592
-48.468
62.053
-6.742
-42.875
-101.901
-30.377
-101.901
134.323
-24.259
如果需要,您可以放入脚本。