ksh help

Hi,

I have command like this with ooutput :

unix>tnsping abc
TNS Ping Utility for IBM/AIX RISC System/6000: Version 10.2.0.4.0 - Production on 17-AUG-2011 17:06:11

Copyright (c) 1997,  2007, Oracle.  All rights reserved.

Used parameter files:
/opt/oracle/network/admin/sqlnet.ora


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)(KEY = key))) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = host.domain)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = instance_name) (INSTANCE_NAME = instance_name)))
OK (20 msec)

I want to parse it to get port number ..

e.g. tnsping abc | grep PORT =

any help ...

Thanks

I'd probably use sed.

command | sed 's/.*PORT =/PORT =/; T del; s/).*//; t; :del d'
grep -Po '(?<=PORT = )\d+'

Both of them didn't work.

Thanks

What was the output that was produced, or the errors generated? Both work for me on Linux.

TNS Ping Utility for IBM/AIX RISC System/6000: Version 11.2.0.2.0 - Production on 17-AUG-2011 22:11:20

Copyright (c

Used parameter files:


Used TNSNAMES adapter to resolve the alias
PORT = 2100
OK (40 msec

The output has multiple lines . need just value of port ( in this case 2100) .
Appreciate your help.

Thanks

 
your-command | nawk -F"=" '/PORT/ {print $2}'

I'm embarrassed! I went back and discovered I tested only against the line with PORT and not the whole file. Grrr!!

The awk itkamaraj posted is much cleaner, but it can be done in sed:

your-command| sed 's/.*PORT =/PORT =/; T del; s/).*//; t; :del d'

Thanks

by combining 2 commands it's giving the output :

tnsping abc | sed 's/.*\(PORT =\)/\1/; s/).*//' | nawk -F"=" '/PORT/ {print $2}'

3202  

this the value of port ( and this is what I want)

is there way to combine these two ?

Thanks

Does this not work by itself?

tnsping abc | sed 's/.*PORT =//; T del; s/).*//; t; :del d'

I noticed you had my old, and very wrong, original post. This is the one that I believe should do the trick.

---------- Post updated at 23:55 ---------- Previous update was at 23:53 ----------

I also see that itkamaraj's awk works only on the output of my bad command, not on the output of your command. Didn't catch that before.

---------- Post updated at 23:59 ---------- Previous update was at 23:55 ----------

And if that sed doesn't work (I have a version of sed that doesn't like that syntax), here's an awk that should work in every environment:

tnsping abc | awk '/PORT =/ { gsub( ".*PORT =", "" ); gsub( ").*", "" ); print; }'

Thanks for your help . It's still failing ...

tnsping abc | awk '/PORT =/ { gsub( ".*PORT =", "" ); gsub( ").*", "" ); print; }'
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1

Thanks

 
$nawk -F"[)(]" ' /PORT/ {for(i=1;i<=NF;i++){if($i~/PORT/)print substr($i,index($i,"= ")+2,length($i))}}' test                                             
1521
 
$ cat test 
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)(KEY = key))) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = host.domain)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = instance_name) (INSTANCE_NAME = instance_name)))
OK (20 msec)
 
awk '/PORT =/ { gsub( ".*PORT =", "" ); gsub( ").*", "" ); print; }'

this is not working on Solaris . Is this because of gsub ?

Thanks

Yes. Use nawk or /usr/xpg4/bin/awk on Solaris.

Thanks..But, it's not working ...

tnsping abc | nawk '/PORT =/ { gsub( ".*PORT =", "" ); gsub( ").*", "" ); print; }'
nawk: illegal primary in regular expression ).* at .*
 source line number 1
 context is
        /PORT =/ { gsub( ".*PORT =", "" ); gsub( ").*", "" >>>  ) <<< 

Thanks

I've always found /usr/xpg4/bin/awk to be a bit more "reliable" than nawk. Please try that.

(and please also use code tags for terminal output :))

Maybe not the best sed, but does the trick:

$ tnsping DB1 | sed -n "s/.*PORT = \([^)]*\).*/\1/p" 
1521

Thanks

---------- Post updated at 01:49 PM ---------- Previous update was at 11:14 AM ----------

sed -n \"s/.*PORT = \\([^)]*\\).*/\\1/p\""

if I want to check PORT=, PORT =, PORT = all in one condition , is this possible ?

Thanks

Sure! An asterisk (*) in a RE means "zero or more":

sed -n \"s/.*PORT *= *\\([^)]*\\).*/\\1/p\""

it's not working . I will explain bit more . The output sometimes comes back like this :

PORT=
PORT =
PORT =

need to check for above three contions.
Scot, the solution you provided is not working.

Thanks

Why did I cut-and-paste the command from your last post into my reply?

Where did you get that command from?

$ tnsping DB1 | sed -n "s/.*PORT *= *\([^)]*\).*/\1/p"
1521

Works fine.