I want to fetch 10.2.0.4.0 from tnsping output in AIX, so I wrote tnsping tucson | grep Version | awk '{print $9}', but sometimes in other platforms like Linux the column varies, so I cannot use {print $9} everywhere. Is there a way such that

  1. The command looks for Version
  2. and then just after it prints whatever number comes up (including ".")

Or any other methods?

TNS Ping Utility for IBM/AIX RISC System/6000: Version 10.2.0.4.0 - Production on 16-NOV-2011 16:19:02

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

Used parameter files:

Used HOSTNAME adapter to resolve the alias
Attempting to contact (DESCRIPTION=(CONNECT_DATA=(SERVICE_NAME=bluebird.informatica.com))(ADDRESS=(PROTOCOL=TCP)(HOST=10.65.40.235)(PORT=1521)))
TNS-12535: TNS:operation timed out

I got it for AIX, HPUX, and Linux:

tnsping tucson | grep Version | awk -F Version  '{print $2}' | awk '{print $1}'

But I get an error on SunOS I want something that will work on Linux, HP-UX, AIX, and SunOS.

zenvo%  tnsping tucson | grep Version | awk -F Version  '{print $2}' | awk '{print $1}'
awk: syntax error near line 1
awk: bailing out near line 1

Update after Shelter's answer

Linux, AIX and HP-UX:

tnsping tucson | grep Version | awk -F Version  '{print $2}' | awk '{print $1}'

SunOS:

tnsping tucson | grep Version | nawk -F Version  '{print $2}' | nawk '{print $1}'
有帮助吗?

解决方案

AWK on Solaris is old AWK. Usually, Solaris people use nawk to get the benefits of 'new' AWK.

I don't think old AWK supports a multi-character value as a field separator as you defined -F Version (nice trick!). If you're lucky, add quotes around that value might solve the problem.

More likely, you have to make your script smart to assign nawk when running on Solaris.

Something like

case $( uname -a ) in
    *Solaris* ) awk=nawk ;;
    * ) awk=awk
esac

tnsping tucson | grep Version | ${awk} -F "Version"  '{print $2}' | ${awk} '{print $1}'

should do the trick. I don't have access to a Solaris system anymore, so I can't verify that this will work, but I'd be surprised if it doesn't.

Hmm, now something from my past tells me that even nawk on Solaris didn't like strings as FieldSeps. Maybe you can get awksome gawk installed OR another solution (it seems you're on the way there), just

tnsping tucson | ${awk} '/Version/ {sub(/,*Version /, "", $0) ; sub(/.*$/, "", $0); print $0; exit}'

and you should be whittled down to your version number anyway. Also, with the filter in AWK for '/Version/', you don't need the grep in between AND we've eliminated an extra call to AWK.

I hope this helps.

其他提示

Please try with sed:

tnsping tucson |
sed -n '/Version/{                                                       # Grep like
    s@.* Version \([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*@\1@i  # Substitution with @ separator
    p;                                                                   # Print
    q;                                                                   # quit
}'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top