![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Parse String Using Sed | racbern | Shell Programming and Scripting | 4 | 04-23-2008 01:14 PM |
| how to parse this string | hcliff | Shell Programming and Scripting | 13 | 04-02-2008 05:43 AM |
| Parse | mirusnet | Shell Programming and Scripting | 1 | 01-23-2008 06:49 PM |
| parse xml | bin-doph | High Level Programming | 5 | 03-15-2004 11:19 AM |
| Parse | nguda | Shell Programming and Scripting | 7 | 05-16-2002 10:10 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
How to parse..
Help, I need to get the port number of a Oracle database using the tnsping command. I need to parse it's output.
===================== Attempting to contact (ADDRESS=(PROTOCOL=TCP)(Host=chamar)(Port=1541)) Sometimes may be like this: Attempting to contact (ADDRESS=(COMMUNITY=TCP.WORLD)(PROTOCOL=TCP)(Host=chamar)(Port=1541)) ===================== Also, sometimes Port will be PORT or port. I tried using awk and delimiting by = but sometimes there are more = than I expect.. Thanks for your help. Last edited by natter; 05-06-2003 at 05:22 PM.. |
|
||||
|
Code:
tnsping PROD | awk '/Attempting/ {
gsub("[()=]"," ")
$0=toupper($0)
for (i=1;i<=NF;i++)
if ($i=="PORT")
{print $(i+1)
exit}
}' | read port
echo "port=$port"
I think the line of interest is always just one line, correct? You show one sample as being on two lines. If so, just change the first line to: tnsping PROD | awk '{ and it will parse each line until PORT is found. |
|
|||||
|
Assuming your awk is nawk, you can (probably) do this:
Code:
tnsping PROD | awk '/Attempting/ {
s = toupper($0)
if (!match(s, /PORT=[0-9]+/)
print "Where's the port?" > "/dev/tty"
exit(1)
else
port = substr(s, RSTART+5, RLENGTH-5)
}' | read port
echo "port = " $port
If your output may actually come out on multiple lines per the formatting of your original post, but the "port=" and its port number will always be on the same line, you can shortcut the above this way: Code:
tnsping PROD | awk 'match(toupper($0), /PORT=[0-9]+/) {
print substr($0, RSTART+5, RLENGTH-5)
}' | read port
echo "port = " $port
|
|
||||
|
I like to use awk because its pretty powerful, but I also like to try to achieve the same results with other tools. Here is my bid which works every time:
$ echo "Attempting to contact (ADDRESS=(PROTOCOL=TCP)(Host=chamar)(Port=1541))" |tr -d ")" | cut -d"(" -f5 | cut -d"=" -f2 Good luck! |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|