Hi
I need to pass an array to Awk script from Shell. Can you please tell how to do it? How to pass this array add_ct_arr to an awk script or access it in awk?
i=1
while [ $i -le $entry_count ];
do
add_ct_arr[$i]=$(echo ${adda_count} | awk -v i=$i -F" " '{print $i;}')
echo ${add_ct_arr[${i}]}
i=`expr $i + 1`
done
awk 'NR==FNR{add_ct_arr[i\+\+]=$0;next}{print $0;}' file.txt > new_file.txt
Please put code inside
tags.
i=1
while [ $i -le $entry_count ];
do
add_ct_arr[$i]=$(echo ${adda_count} | awk -v i=$i -F" " '{print $i;}')
echo ${add_ct_arr[${i}]}
i=`expr $i + 1`
[/quote]
[INDENT]
The shell can do integer arithmetic; you don't need to call an external program:
i=$(( $i + 1 ))
I'd pass the array as a single variable and let awk split it:
## I've used a comma as a separator; use whatever is appropriate
array=$( printf "%s," "${add_ct_arr[@]}" )
awk -v add_ct_arr="$array" 'BEGIN { split(add_ct_arr,ct_array,",") }
...'