saving values from awk expression into shell array

hi i am trying to save the values i extract from a file with the help of awk in a bash shell array.
i have:

exec 10<file2 
while read LINE <&10; do
   
    ARRAY1[$count]=$(awk '{print $1}' file2)
    ((count++))
done
echo ${ARRAY1[@]}

it prints just blank lines. file1 has two columns and i am trying to save the first column into an array..

this is not working.. any ideas what is wrong here?

thanks

What is in file2?

If it is file1 that has two columns, why are you reading from file2?

There are so many things wrong with that piece of code that it's hard to know where to begin.

That code is reading the entire file into an array for every line of the file. I tried it with a 9-line file, and it created 81 lines of output. If you got blank lines, you are probably reading the wrong file.

If you want to put the first field of a file into an array, there are many ways to do it. For example, using a while loop as you have:

count=0
FILE=file1
exec 3<"$FILE" ## Some shells only support FDs <= 9
while read a b <&3; do ## split the line as you read it
    ARRAY1[$count]=$a
    count=$(( $count + 1)) ## Use standard syntax for arithmetic
done
printf "%s\n" "${ARRAY1[@]}"

More efficient is:

## Assuming that the fields are separated by spaces
## Change the delimiter (and possibly IFS) if using something else
 
ARRAY1=( $(cut -d ' ' -f1 file1) )

eval `awk '{print "arra["NR-1"]="$1}' a`
echo ${arra[@]}

thanks. i used the following:

ARRAY1=( cut -d ',' -f1 file1 )

file1 has the following:

1,5
2,9
3,8

does the first column from file1 get saved as array elements? because when i did echo ${ARRAY1[1]} i got a blank line. what am i doing wrong?

Sorry, my mistake. That should have been:

ARRAY1=( $(cut -d ',' -f1 file1) )