saif
1
Hi guys, I am not an expert in shell and I need help with awk command. I have a file with values like
200 1 1
200 7 2
200 6 3
200 5 4
300 3 1
300 7 2
300 6 3
300 4 4
I need resulting file with averages of col 2 and 3 based on values on col 1. So the output should be like
200 4.5 2.5
300 5 2.5
I would really appreciate any help.
Thanks,
awk '{a[$1]+=$2; b[$1]+=$3; c[$1]++} END{for(i in a) print i, a/c , b/c}' file
If you want the keep the order of the 1st column:
awk '{a[$1]+=$2; b[$1]+=$3; c[$1]++} END{for(i in a) print i, a/c , b/c|"sort -n"}' file
or gawk:
WHINY_USERS=1 gawk '{a[$1]+=$2; b[$1]+=$3; c[$1]++} END{for(i in a) print i, a/c , b/c}' file
or without the external sort:
nawk '!($1 in a) {key[++key[0]]=$1}{a[$1]+=$2; b[$1]+=$3; c[$1]++} END{for(i=1;i<=key[0];i++) print key, a[key]/c[key] , b[key]/c[key]}' myFile
saif
4
Thank you so much!!. It worked;