The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-06-2003
natter natter is offline
Registered User
  
 

Join Date: Oct 2001
Location: Denver
Posts: 25
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..
  #2 (permalink)  
Old 05-07-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
You could use the substr function of awk:

awk '{ print substr ($0, length ($0) - 5, 4)}' someFile

or:

port="Attempting to contact (ADDRESS=(PROTOCOL=TCP)(Host=chamar)(Port=1541))"
echo $port | awk '{ print substr ($0, length ($0) - 5, 4)}'
  #3 (permalink)  
Old 05-07-2003
Jimbo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
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"
Above script simplifies the parsing task by first cleaning up the line, then scans the line for PORT and prints the word after. Port number can be more than 4 digits.

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.
  #4 (permalink)  
Old 05-10-2003
natter natter is offline
Registered User
  
 

Join Date: Oct 2001
Location: Denver
Posts: 25
Great, thanks very much for the replies.

I'll try both ways. I was stuck.
  #5 (permalink)  
Old 05-14-2003
criglerj's Avatar
criglerj criglerj is offline
Registered User
  
 

Join Date: May 2002
Location: Atlanta
Posts: 129
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
The regexp may need to be adjusted to /PORT=[0-9][0-9]*/ if your awk doesn't like the +.

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
The secret here is to realize that the pattern does not have to be a regular expression. Be a little careful: There's only one RSTART/RLENGTH pair, so more than one "match" in an expression (in the pattern or the action) will only get set RSTART & RLENGTH for the last "match" evaluated.
  #6 (permalink)  
Old 05-21-2003
biker_mike biker_mike is offline
Registered User
  
 

Join Date: May 2003
Posts: 3
Smile

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!
  #7 (permalink)  
Old 05-21-2003
biker_mike biker_mike is offline
Registered User
  
 

Join Date: May 2003
Posts: 3
Talking

This is even better:

$ echo "Attempting to contact (ADDRESS=(PROTOCOL=TCP)(Host=chamar)(Port=1541))"
|tr -d ")" | cut -d"=" -f5

Should've thought of this the first time.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:47 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0