awk to print exact field number

Hello there.
I want some help with awk
I have this script that reads a file from awk and you can insert from keyboard any list from the fields that exist so to be printed on monitor

echo "give a choice"
read -a ans
array=${ans[@]}
awk -F: -v k="$array" '{
                         split(k,l," ")
                         for(i=1;i<=NF;i++)
                            {          
                                         printf("%s \t",$l)
                               
                             }  printf("\n")
                       }' /etc/passwd                                    

I get the field numbers from an array and then I split this array in awk.
If I try to print "l[i]" it works but when I try to print it as field number I dont get the correct result.

I want my outpout to be something like this :

for example:
>give a choice
:1 5 3
and then I will like to print the first field in each line then the 5th and then the 3rd
first fifth third
first fifth third
first fifth third
............

don't use "$" while printing the array element

if i dont it prints the number that i have given ... not the first and the second field for example but the number 1 and 2.

sorry my mistake... use this..

awk -v var="2 5 7" '{split(var,a," ")
for(i=1;i<=(length(var)+1)/2);i++){printf $(a)"\t"}{printf "\n"}}' filename

I can't really understand why we take this "i<=(length(var)+1)/2" in for but it works great.
Thanx a lot :slight_smile: :slight_smile: