想要分隔不同的值并使它们成为变量:
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:无法解析名称”并在其后添加未知的字符串因为没有额外的数据。
预期观点:
db2,active,server1
db1,inactive,10.1.1.1
xdb3,inactive,unknown
答案 0 :(得分: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
答案 1 :(得分: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
文件句柄。