我编写了一个Perl脚本,它解析命令的输出,在命令输出中查找任务名称并打印任务名称。
命令输出如下所示
此处任务名称为“mutipleregexpression”。但是“压力”这个词的一部分会在新行中打印出来。
Task Name: multipleregex
pression
这是我目前在我的代码中使用的正则表达式:
`$line =~ /(.*):\s*(.+)/`
上述正则表达式不会捕获在新行中打印的任务名称的一部分。它只捕获'multipleregex'它不会捕获'压力',因为压力会在新行中打印出来。 我看到以下错误
Use of uninitialized value $result in concatenation (.) or string at line 114.
所以我的问题是我需要对正则表达式做出哪些改变,以便它能够捕获'压力'
捕获任务名称的功能
sub get_status {
my ($self) = @_;
my $taskname;
my %result;
my $line;
$self->{'cmd'} = # command goes here;
# execute the command
$self->execute($self->{'cmd'});
$self->{'stdout'} = $self->get_stdout();
my $count = 0;
# traverse through the output to parse the output
foreach $line (@{ $self->{'stdout'} }) {
chomp $line;
if ( $line =~ m/^Task\sName\:\s+(\w+)/msx ) {
$taskname = $1;
$count = 0;
}
elsif ($line =~ /(.*):\s*(.+)/) {
my $key = $1;
my $value = $2;
$result{$taskname}{ ++$count } = $value;
}
}
return \%result;
}
函数调用:
my $result = $stats->get_status('multipleregexpression', '1');
INFO('Status of the task is :' . $result );
输出
Use of uninitialized value $result in concatenation (.) or string at line 114.
20150317T194029 INFO Status of the task is :
答案 0 :(得分:0)
我也不懂语言,但你可以尝试这个逻辑:
var current_line = empty string.
foreach LINE in lines {
if LINE contains ":" {
then
1. Save or do something with last line (current_line).
2. LINE is the start of a new line (current_line = what comes after ":").
}
else {
then
1. LINE is a continuation of last line
(current_line = current_line + LINE).
}
}
希望它有所帮助。