Find the answer to your Linux question:
Results 1 to 4 of 4
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" ...
  1. #1
    Just Joined!
    Join Date
    Aug 2007
    Posts
    1

    extraction using awk or sed

    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.

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    welcome to the forum

    not sure what you intend about the array--this will
    get the "HOST=x.1"s

    'sed 's/\o40//g' <file | cut -c25-32'

    where "file" is your 3 line example
    the sun is new every day (heraclitus)

  3. #3
    Just Joined!
    Join Date
    Aug 2007
    Posts
    2
    A small correction to what tpl suggested:

    If the HOST value is 10.1, it may go wrong. So we can rewrite it as
    awk -F "=" '{ print $4}' file |cut -d')' -f1 |sed 's/^\ //g'
    where 'file' is the input file.

    This will only print the value corresponding to the HOST.

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    you can do most of the thing in awk.
    Code:
    awk '{ gsub(/.*HOST/,"");
           gsub(/PORT.*/,"");
           gsub(/[()= ]/,"");       
           ip[c++]=$0
        }
        END {
    	#dosomething with ip array
        }
    ' "file"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...