•
•
•
•
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
![]() |
•
•
Join Date: Aug 2007
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
$ 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.
•
•
Join Date: Aug 2007
Posts: 57
Reputation:
Rep Power: 2
Solved Threads: 2
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/ *).*//'
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- AWK Extracting Multiple Lines (Shell Scripting)
- help with shell script padding files with spaces (Shell Scripting)
- Regex for password (Shell Scripting)
- how long does it take to learn assembler and how difficult is it? (Assembly)
- Need help getting specific info out of a file (*nix Software)
- Help with SED/AWK email parser (Shell Scripting)
- Good books on Shell Programming/Perl (Perl)
Other Threads in the Shell Scripting Forum
- Previous Thread: ssh shell script
- Next Thread: Error while reading value from user in shell script


Linear Mode