![]() |
|
#1
|
|||
|
|||
extraction using AWK or SEDHi 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. |
|||
|
#2
|
|||
|
|||
Re: extraction using AWK or SEDQuote:
The reason that I like problems like this is that there are about a million ways (maybe more) of doing it, but if you can't figure out the first way, well...you're stuck. On the other hand, if someone points you in the direction of a way of doing it, you will (probably) immediately say, "OK, but I think a "better" way would be..." All of the ways that come to (my) mind involve regular expressions. I really like regular expressions, especially used in shell command lines. (See footnote.) The title of this thread indicates your predilection for awk or sed, so I am sure that you are aware of regular expressions. Awk is good at regular expressions and so is sed. I like awk for certain applications, like when I want to select arguments that are predictably set off by spaces. In this case they are not, so you could just use awk's regular expression matching and substitution on whole lines of the file. Or, you could just use sed, as I will show. (My examples are for bash, which is the default shell for my Linux distribution.) I did it in two steps (maybe you would like to try to combine them, but I'll keep it the way that I thought of it): 1. Eliminate everything up to and including the value of the HOST field. Put the following in a script file (or just enter the sed command line at a bash shell command prompt): Code:
With "inputfile.txt" containing the three lines of your example, here is the result: Code:
2. Now, eliminate everything after the numeric field: Code:
Output: Code:
Now if you want to create some textual output as you indicated in your post, you could do something like: Code:
Output: Code:
Regards, Dave Footnote: Everyone has heard of the "thought experiment" where one would put an infinite number of monkeys in a room with typewriters (it's a very old experiment). The idea was that there is a non-zero probability that at least one would end up typing the complete works of Shakespeare. In fact an experiment was carried out recently with only 50 monkeys . They were put into a room with keyboards connected to computers running Linux. Of course there wasn't much hope of seeing the complete works, but maybe one would come up with, perhaps, a single line of some Shakespeare play or sonnet. Upon checking after about 10 minutes: no Shakespeare was evident, but every single one had come up with several valid Linux shell commands. ---davekw7x |
|
#3
|
|||
|
|||
Re: extraction using AWK or SEDThank you so much sir, you have been of great help.
You are the saviour of a newbie job. Thanks a zillion!!!!!!!!!! Harris. |
Recent GIDBlog
Problems with the Navy (Officers) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| ? re input from stringstream | earachefl | C++ Forum | 3 | 01-May-2006 15:54 |
| How to count lines in a file without extraction? | earachefl | C++ Forum | 6 | 21-Apr-2006 15:46 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The