从多行输出中捕获值

时间:2012-01-28 19:22:27

标签: perl

use Control::CLI;
$cli = new Control::CLI('SSH');
$cli->connect(Host=>'10.10.10.10',Username=>'user',Password=>'pwd');
$cli->waitfor('>');
$cli->print('Show XXXXXXXXXXXXXXXXXXXX| grep Active');
@f=$cli->waitfor('>');
print @f;
$cli->disconnect;

上面的代码输出为:

 Show XXXXXXXXXXXXXXXXXXXX| grep Active
Active
Sunday January 29 00:40:34 IST 2012
Active                  : 1106          Dormant                 : 0
[local]NOI-S57-XYXYX>

但是我想只捕获“1106”(在第3行,Active之间的值:“1106”和Dormant),请帮助!!

提前致谢!!

3 个答案:

答案 0 :(得分:1)

更具体地说,对于LINE-4:

my $number = $1 if $f[3] =~ m/Active\s+:\s+(\d+)\s+Dormant/;
print defined $number ? "$number\n" : "NONE\n";

答案 1 :(得分:0)

通过最少的错误检查,这应该在第三行得到你的号码:

if( $f[3] =~ m/(\d+)/ ) {
  printf "Found number on third line: %d\n" , $1 ;
} else {
  print "No number on line 4\n" ;
}

编辑:更正的行号

答案 2 :(得分:0)

以下代码对我有用:

@myarray = ($f =~ m/(\d+)/g);
print "@myarray['5'] ";