Since the awk body is in single quotes, awk does not see the shell variable. awk thinks you mean an awk variable i. That variable is undefined, so in a numeric context it has the value zero. Thus $i is interpreted by awk as $0, or the whole line.
You need to pass the shell variable into awk.
for ((i=1; i<=$count; i++))
do
abc=$(awk -F "," -v col=$i 'NR==1 {print $col}' $file)
#................^^^^^^^^^...............^^^^
echo "$abc"
done
However, there's a better way than calling awk 3 separate times:
while IFS=, read -ra fields; do
printf "%s\n" "${fields[@]}"
# or do stuff with the individual fields
for ((i=0; i < ${#fields[@]}; i++)); do
echo "${fields[i]}"
done
done < "$file"
- the
whileloop, with the file's contents redirected into it, loop over the lines of the file IFS=,instructs thereadcommand to do word splitting on commasread -rmeans to not interpret any special characters as anything but regular chars.read -a fieldsreads the words in the line into an array variable namedfields