I am new in bash scripting and trying to solve one problem for my data file.
I have a file that has three columns, the pattern of the file is as follows
#col1 #col2 #col3
1.1 0.0 2.1
1.2 0.0 2.2
1.3 0.0 2.3
0.9 0.0 0.3
0.8 0.0 0.4
0.7 0.0 0.4
2.1 0.0 0.6
2.1 0.0 0.6
2.2 0.0 0.7
and it has more than 1000 terms.
I have to take the average of the third column. For this, I have to read the file in one loop and then consider the space, and do the same for the second loop, consider the space, and then loop for the third column.
I wrote a small script but I am not able to understand how I can start the second iteration again and calculate the average.
IFS=$'\n'
count=0;
total=0;
for i in $( awk '{print $3;}' Berry_Ef)
do
k=$(awk -v n=$i 'NR==n{print $3}' )
total=$( echo $total $i | bc)
((count ))
done
echo "$count" "count">> sum
echo "$total" "total">>sum
echo "scale=5; $total/$count" | bc >>sum
Any suggestion and help matter to me.
CodePudding user response:
Suggesting to use single awk
script to read and process input file only once.
awk ' {
col3Count;
total = total $3;
}
END {
printf("total = %d count = %d normal = %d\n", total, col3Count, (total/col3Count));
}
' input.csv