User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,428 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,581 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 2142 | Replies: 2
Reply
Join Date: Aug 2007
Posts: 1
Reputation: harris2107 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
harris2107 harris2107 is offline Offline
Newbie Poster

value extracting using awk or sed

  #1  
Aug 19th, 2007
Hi all,
I have a file (tnsnames.ora in Oracle) from which i have to extract the values of two fields. Suppose following is the content of my file called "inputfile.txt"

(ADDRESS = ( PROTOCOL = TCP ) ( HOST = 1.1 ) ( PORT = 1521 ))
(ADDRESS=(PROTOCOL=TCP)(HOST=2.1)(PORT=1521))
(ADDRESS = ( PROTOCOL = TCP ) ( HOST = 3.1 ) ( PORT = 1521 ))

I want to extract the value of HOST from this file and assin it to an array. For eg. from the above file, i need to get the HOST values and store in the array like

ip[1]=1.1
ip[2]=2.1
ip[3]=3.1

That is i want to extract just the values of HOST from all the lines in the file given. Remember the format is the same but there are spaces in the first and third row but not in the second row. Any help would be greatly appreciated.

Thank you,
Harris.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2007
Posts: 52
Reputation: radoulov is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
radoulov's Avatar
radoulov radoulov is offline Offline
Junior Poster in Training

Re: value extracting using awk or sed

  #2  
Aug 19th, 2007
$ cat file
(ADDRESS = ( PROTOCOL = TCP ) ( HOST = 1.1 ) ( PORT = 1521 ))
(ADDRESS=(PROTOCOL=TCP)(HOST=2.1)(PORT=1521))
(ADDRESS = ( PROTOCOL = TCP ) ( HOST = 3.1 ) ( PORT = 1521 ))
$ awk '{x[++c]=$5}END{for(i=1;i<=c;i++)print "ip["i"]="x[i]}' FS="[=)] *" file 
ip[1]=1.1 
ip[2]=2.1
ip[3]=3.1 

Use nawk on Solaris.
Last edited by radoulov : Aug 19th, 2007 at 9:33 am.
Reply With Quote  
Join Date: Aug 2007
Posts: 57
Reputation: Fest3er is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
Fest3er Fest3er is offline Offline
Junior Poster in Training

Re: value extracting using awk or sed

  #3  
Aug 23rd, 2007
And an example using sed and shell. It can be put on one line also, but I broke it up for readability:

sed -e 's/.*HOST *= *//' -e 's/ *).*//' inputfile.txt | (
typeset -i i; i=1
while read a; do
echo "ip[$i]=$a"
i=i+1
done
)

Because a 'freak' wrote bc(1) in sed some years back, I'm sure the whole thing could be done in sed.

A shorter alternative using cat and sed:
cat -n a.a | sed -e 's/^ *\([0-9]*\)\t/ip\[\1\]=/' -e 's/[^]]*HOST *= */=/' -e 's/ *).*//'
Last edited by Fest3er : Aug 24th, 2007 at 12:06 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 1:19 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC