I'm trying to read a file which is in pairs as follows:
V1#K1.@
V2#K1.@
V3#K2.@,V4#K1.@,V5#K2
V1#K3.@
My aim is to store it in key<=>pairs
with #
as a delimiter after removing '@'
Value is placed before #
and Keys are after #
in the example file
The answer mentioned in associate multiple values for one key in array in bash couldn't be implemented. So i tried it in the following way in ksh:
#!/usr/bin/ksh
typeset -A arr
while IFS= read -r line;do
STRIPPED=`echo $line|sed 's/.@//g'`
OIFS="$IFS"
IFS=','
read -A TOKENS <<< "${STRIPPED}"
IFS="$OIFS"
for key in ${TOKENS[@]};do
echo "Token is $key"
arr[${i##*#}]=${i%%#*}
echo "Key: ${key##*#}, Value: ${arr[${key##*#}]}"
done
done <MYFILE
# Printing key and its values
for i in ${!arr[@]};do
echo "key: ${i}, value: ${arr[$i]}"
done
But this overwrites the previous values for a key. It doesnt consider multiple values for a key. Is there a way to do it in ksh(not bash)?