please Write a shell script

Hi Team,

I am unable to write script. please guide me. My rquirement is as given below

one file will have three columns with n number of rows like
hostname port sid
-------- ---- ---
sun056 1527 PSP1
sun111 1529 PRP1
sun107 1580 PRD1

the script should read all rows in a column and each row should change to like below given format . all will goes to one file.

PSP1-SUN056.WORLD =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (COMMUNITY = TCP)(PROTOCOL = TCP)(HOST = sun056)(PORT = 1527))
)
(CONNECT_DATA =
(SID = PSP1)
)
)

Please write a shell script and send it to me.

Thanks
Venkat

use

#! /usr/bin/ksh

while read line
do

hostname=`echo $line | cut -d" " -f1`
port=`echo $line | cut -d" " -f2`
sid=`echo $line | cut -d" " -f3`

echo "PSP1-SUN056.WORLD =\n (DESCRIPTION = \n (ADDRESS_LIST =" >>oraconn
echo "(ADDRESS = (COMMUNITY = TCP)(PROTOCOL = TCP)(HOST = $hostname)(PORT = $por
t))\n )\n (CONNECT_DATA =\n (SID= $sid\n)\n)" >>oraconn
done < $1

awk '{ printf("%s-%s.WORLD =\n(DESCRIPTION =\n(ADDRESS_LIST =\n(ADDRESS = (COMMUNITY = TCP)(PROTOCOL = TCP)(HOST = %s)(PORT = %s))\n)\n(CONNECT_DATA =\n(SID = %s)\n)\n)\n",$3,$1,$1,$2,$3)}' file

Hi aju_kup,

thanks. Its working but one problem is there i.e file last line is not reading.
Suppose 32 lines there then its reading 31 lines only. Please tell me.

Thanks all for your support.

Thanks
Venkat

I think there is blank line at end of input file. use the following

#! /usr/bin/ksh

while read line
do

if [[ ! -n $line ]]
then
exit
fi

hostname=`echo $line | cut -d" " -f1`
port=`echo $line | cut -d" " -f2`
sid=`echo $line | cut -d" " -f3`

echo "$sid-$hostname.WORLD =\n (DESCRIPTION = \n (ADDRESS_LIST =" >>oraconn
echo "(ADDRESS = (COMMUNITY = TCP)(PROTOCOL = TCP)(HOST = $hostname)(PORT = $por
t))\n )\n (CONNECT_DATA =\n (SID= $sid\n)\n)" >>oraconn
done < $1

try this....

eecho "hostname port sid^J-------- ---- ---^Jsun056 1527 PSP1^Jsun111 1529 PRP1^Jsun107 1580 PRD1^J" | sed 1,2d | \
awk '{ print $3 "-" $1 ".WORLD =(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (COMMUNITY = TCP)(PROTOCOL = TCP)(HOST =" $1 ")(PORT = " $2")))(CONNECT_DATA =(SID = " $3 ")))" }'

The cskumar solution with simple data validation (check port number) :

awk '
     BEGIN {
        fmt =      "%s-%s.WORLD=\n(DESCRIPTION =\n(ADDRESS_LIST =\n"
        fmt = fmt "(ADDRESS = (COMMUNITY = TCP)(PROTOCOL = TCP)(HOST = %s)(PORT = %s))\n"
        fmt = fmt ")\n(CONNECT_DATA =\n(SID = %s)\n)\n)\n";
     }
     $2 ~/^[0-9]{1,}$/ { 
        printf(fmt, toupper($3) ,toupper($1) ,$1, $2, $3)
     }' file

Jean-Pierre.