awk based script to find the average of all the columns in a data file

Hi All,

I need the modification for the below mentioned code (found in one more post http://www.unix.com/shell-programming-scripting/27161-script-generate-average-values.html\) to find the average values for all the columns(but for a specific rows) and print the averages side by side.

I have the below input format.

H1,H2,H3,H4
1,2,3,4
1,3,4,5
2,4,5,6
3,4,5,6
5,6,7,8
5,5,4,5

I got the below code which gives the average of all rows in every column and printing the output like below.
Avg.of.col1,avg.of.col2,avg.of.col3,avg.of.col4

FNR==1 { nf=NF} {  for(i=1; i<=NF; i++)    arr+=$i   fnr=FNR } END {   for( i=1; i<=nf; i++)    printf("%.2f%s", arr / fnr, (i==nf) ? "\n" : FS) }

But I need the below kind of code (complete shell script)

awk -F,  '$1==specificvalue{Code for average of all columns and print one by one}' inputfile.

The variable specificvalue mentioned in the script is automatically generated by a for loop.

Regards
Sidda

What should be the desired output?

Hi Franklin,

I need the below output for the input data provided.

1,2.5,3.5,4.5
2,4,5,6
3,4,5,6
5,5.5,5.5,6.5

(or)

2.5,3.5,4.5
4,5,6
4,5,6
5.5,5.5,6.5

preferably the first one.

I am running my script currently like this.

for i in $(seq 0 12); do # As all my files are having $1 from 0 to 12

awk -F, -v var1="$i" '$1==var1' file | awk -F, -f 1.awk

done

where my 1.awk is

FNR==1 { nf=NF} {  for(i=1; i<=NF; i++)    arr+=$i   fnr=FNR } END {   for( i=1; i<=nf; i++)    printf("%.2f%s", arr / fnr, (i==nf) ? "\n" : FS) }

Though I am getting desired output, I want to avoid the "|" and merge both the statements into a single command.

Thanks
Sidda

like this?

# cat input
H1,H2,H3,H4
1,2,3,4
1,3,4,5
2,4,5,6
3,4,5,6
5,6,7,8
5,5,4,5
# awk -F, '$1==x&&NR>1{f=NF;for(i=1;i<=NF;i++)a+=$i}END{for(i=1;i<=f;i++)printf "%10s" ,a/(NR-1);print ""}' x=$yourvalue input

i have the following data

0 ORANGE 751 BLUE 885
1 RED 787 GOLD 892
2 ORANGE 298 BLUE 138
3 RED 395 GOLD 755
4 ORANGE 658 GOLD 880
5 RED 79 BLUE 323
6 RED 977 GOLD 410
7 RED 406 BLUE 10
8 RED 15 BLUE 269
9 ORANGE 696 BLUE 623
10 ORANGE 878 BLUE 606

AND i need help displaying it like this

Required Output
TOTAL: 11
# of Red: 6
# of Orange: 5
# of Blue: 7
# of Gold: 4
Avg. of Num A Column: 540.000000
Avg. of Num B Column: 526.454545

I came up with a awk code to help me find the avg of the number column in the terminal but the out put is not what i wont