我有一个文本文件,其中包含已运行,通过和失败的测试数量的信息。
它还包含哪些测试失败的信息。
我想提取已运行且失败的测试总数。
以下是日志文件的示例:
file_1 has difference
file_2 has difference
file_3 has difference
file_4 has difference
file_5 has difference
file_6 has difference
file_7 has difference
file_8 has difference
events has difference
QShrink has difference
Total tests run = 10
Total tests passed = 0
Total tests failed = 10
我试图像这样捕获它,但没有用:
if ( $_=~/^# run =/ || $_=~/^# failed =/ ) {
print $_;
my $entry = <FILE>;
print $entry;
}
我的目标是我应该只能获取相应的数字而不是整个字符串。
答案 0 :(得分:7)
您应该将整行放入模式中,并根据=
之前的最后一个单词来识别。这使它变得灵活,因为如果所有线都存在,你就不需要关心。
use strict;
use warnings 'all';
use Data::Dumper;
my %stats;
while (<DATA>) {
if ( m/^Total tests ([a-z]+) = (\d+)/ ) {
$stats{$1} = $2;
}
}
print Dumper \%stats;
__DATA__
file_1 has difference
file_2 has difference
file_3 has difference
file_4 has difference
file_5 has difference
file_6 has difference
file_7 has difference
file_8 has difference
events has difference
QShrink has difference
Total tests run = 10
Total tests passed = 0
Total tests failed = 10
此解决方案使用哈希来存储匹配项。
$VAR1 = {
'failed' => '10',
'run' => '10',
'passed' => '0'
};
让我们来看看你做了什么。
if($_=~/^# run =/ || $_=~/^# failed =/) { print $_; my$entry=<FILE>; print $entry; }
此代码假定$_
中有某些内容。也许您已经打开了文件并正在阅读它。
while (<DATA>) {
if ($_ =~ /.../) {
所以你说如果当前行匹配字符串的开头,#
,空格,单词运行,空格和=
(或者与失败相同,它应该打印整行,然后将下一行分配给仅存在于该块中的词法变量,并打印出来。
此模式与您的输入不匹配,因此永远不会执行该块。如果是这样的话,你就会为匹配的每一行拉开另一行输入。
所有这些都不是你想要的,也不会让你接近数字。
答案 1 :(得分:0)
if($_=~/Total tests run = ([0-9]+)/)
{
print "Total tests run :$1\n";
}
在上面的代码中,你想要的数字是在perls默认变量$ 1中捕获的,因为它们被放在大括号中。同样地,你可以为失败的测试数量做。