I tried to change an string to index to use for an array but I have been unable to get it to work.
This is my file
$ cat file1.txt
101,Harish,BAN
102,Srinu,HYD
And this code:
#!/bin/bash
IFS=','
while read line
do
DELIM_REMOVE=`echo $line|sed 's/,/ /g'`
V=($DEL_REMOVE)
echo ${DELIM_REMOVE}
for i in "${!V[@]}"; do
printf 'V[%s] = %s\n' "$i" "${V[i]}"
echo "${V[i]}"
done
done < /home/ec2-user/file1.txt
echo "${V[i]}"
I also need to use the dynamically generated variables in loop to another loop.
read
fails (on end-of-file) at which point$v
is set to an empty array. If you want to access the array for the last line of input, you'd need to make a copy within the loop (likelast_v=("${v[@]}")
. – Stéphane Chazelas Dec 01 '18 at 10:02ksh93
shell,bash
only supports one-dimensional (sparse) arrays. Or again, use a proper text-processing tool likeawk
orperl
. – Stéphane Chazelas Dec 01 '18 at 12:58