aajan
1
hi All,
Have a doubt in ksh..Am not familiar with arrays but i have tried out a script..
plzzzzz correct me with the script
My i/p File is:
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS =
(PROTOCOL = TCP)
(Host = 192.168.2.2)
(Port = 1525)
)
)
(CONNECT_DATA = (SID = TESTDB1)
)
)
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS =
(PROTOCOL = TCP)
(Host = 192.168.2.4)
(Port = 1525)
)
)
(CONNECT_DATA = (SID = TESTDB2)
And My script is:
while read line
do
i=0
a[$i]=`grep "Host" |cut -d" " -f3 |tr -d ')'`
echo ${a[$i]}
i=`expr $i + 1`
done < File Name
Am trying to get the value of host and assign it to an array
My O/p is
192.168.2.2 192.168.2.4
But i need the o/p to be:
a[0]=192.168.2.2
a[1]=192.168.2.4
awk -F" = " '/Host/ { arr[i++] = substr($2, 0, length($2) - 1) }END{ for ( i in arr ) { print i, arr } }' filename
change the echo statement to
echo "${a[$i]} \n"
aajan
4
Thank Matrix!!!!!!!!!!
But how to store the value in an array
Like
Host[0]=192.168.2.2
Host[1]=192.168.2.4
its already in the array named 'arr'
thats what I have printed in the END block of awk.
Are you looking for displaying it in the format posted ?
aajan
6
Yes Am looking for the Format which i have given in the o/p
With zsh:
zsh 4.3.4 % host=(${$(grep -F Host file)//[^0-9.]/})
zsh 4.3.4 % print $host[1]
192.168.2.2
zsh 4.3.4 % print $host[2]
192.168.2.4
With bash:
bash 3.2.13(1) $ while read;do
> [[ "$REPLY" == *Host* ]]&&host=($host ${REPLY//[^0-9.]/})
> done<file
bash 3.2.13(1) $ echo ${host[0]}
192.168.2.2
bash 3.2.13(1) $ echo ${host[1]}
192.168.2.4
Just modify the print clause in the awk command to,
awk -F" = " '/Host/ { arr[i++] = substr($2, 0, length($2) - 1) }END{ for ( i in arr ) { printf "Host[%d]=%s\n", i, arr } }' filename