Recommand · October 14, 2021 0

AWK result not displaying in the output file

I’m trying to calculate the average of some values base on the input files. It works if it reads 1 file but for some reason it doesn’t work properly when tries to read multiple files. It creates the output file but no contents inside.

Sample input: testFile_a.csv

a,2099/09/21,08:41:12,abcadef,1,0.099,0.123
a,2099/09/21,08:41:29,abcadef,1,0.080,0.088

Here is what I did so far

TYPE=("a" "b" "c")
for ctr in ${TYPE[@]}; do
cat "testFile_${ctr}.csv" | awk -F ',' '{
key=$1","$2","substr($3,1,5);
sum[key]=sum[key]+$5;
sum2[key]=sum2[key]+$7;
}END{
print k","sum[k]","(sum2[k]/sum[k])","sum2[k]
}
}' | sort > "outputFile_${ctr}".csv
done