Kalin Borisov
Kalin Borisov

Reputation: 1120

How to separate different values and make them variables

Want to separate different values and make them variables:

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

Also OK to be renamed to active and when get any different result from that to get inactive like in the case is "TNS:no listener" or "TNS-03505: Failed to resolve name" and to add unkown string after it because don't have additional data.

Expected View:

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

Upvotes: 0

Views: 223

Answers (2)

mpapec
mpapec

Reputation: 50667

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

modified version,

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

Upvotes: 3

TrueY
TrueY

Reputation: 7610

If you already use perl, I may suggest something like this:

#!/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

Output:

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

Of course you should use the input file, instead of the DATA file handle.

Upvotes: 2

Related Questions