0

想要分离不同的值并使它们成为变量:

db2
Attempting to contact (DESCRIPTION= (ADDRESS_LIST= (ADDRESS=(PROTOCOL=TCP)(HOST=server1)(PORT=1521))) (CONNECT_DATA=(SERVICE_NAME=db2))) OK (0 msec)
db1
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL= TCP)(Host= 10.1.1.1)(Port= 1521)) (CONNECT_DATA = (SERVICE_NAME =db1))) TNS-12541: TNS:no listener
xdb3
TNS-03505: Failed to resolve name

也可以将其重命名为活动,并且当得到任何不同的结果以使其处于非活动状态时,例如“TNS:无侦听器”或“TNS-03505:无法解析名称”并在其后添加未知字符串,因为不要' t 有额外的数据。

预期视图:

db2,active,server1
db1,inactive,10.1.1.1
xdb3,inactive,unknown
4

2 回答 2

3
perl -ne 'chomp;push @r,$_}{ ($m)=$r[$_] =~ /host=\s*([^)]+)/i, print ("$r[$_-1],", $m? "active,$m\n":"inactive\n") for grep $_%2, 0..$#r;' logfile

修改版,

perl -ne 'chomp; if($. % 2){print "$_,";next;} ($m)=/host=\s*([^)]+)/i; print ((/\bOK\b/ ? "active," :"inactive,"), $m||"unknown","\n")' logfile
于 2013-06-05T09:59:23.187 回答
2

如果您已经使用 perl,我可能会建议您这样做:

#!/usr/bin/perl

use strict; use warnings;

my $name;
while (<DATA>) {
  if ($. % 2) { chomp; ($name = $_)=~ s/\s+$//; next; }
  if (/HOST=([^)]+)/i) { print "$name,active,$1\n";
  } else { print "$name,inactive\n";
  }
}

__DATA__
db2    
Attempting to contact (DESCRIPTION= (ADDRESS_LIST= (ADDRESS=(PROTOCOL=TCP)(HOST=server1)(PORT=1521))) (CONNECT_DATA=(SERVICE_NAME=db2))) OK (0 msec)
db1    
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL= TCP)(Host= 10.1.1.1)(Port= 1521)) (CONNECT_DATA = (SERVICE_NAME = db1))) OK (0 msec)
xdb3
TNS-03505: Failed to resolve name

输出:

db2,active,server1
db1,active, 10.1.1.1
xdb3,inactive

当然你应该使用输入文件,而不是DATA文件句柄。

于 2013-06-05T09:57:19.783 回答