# 整理：AWK的初级运用之分组计算功能

[root@localhost wms]# cat groupandsum.txt
John|P|physics|2|02/12/2002
Rick|L|Mechanical|1|02/12/2002
Jack|T|electrical|3|03/12/2003
Phil|R|Electrucal|1|03/12/2003
Mike|T|mechamical|2|10/12/2003
Paul|R|chemical|2|10/12/2003
John|T|chemical|3|10/12/2002
Tony|N|chemical|2|10/11/2003
James|R|Electrucal|2|10/11/2003

awk -F '|' '{sum1 += \$4} END {print sum1}' groupandsum.txt

[root@localhost wms]# awk -F "|" '{x[\$5]+=\$4} END{for( i in x ){print i,x[i]}}' groupandsum.txt
03/12/2003 4
02/12/2002 3
10/12/2002 3
10/12/2003 4
10/11/2003 4

[root@localhost wms]# awk -F "|" '{x[\$5"-"\$3]+=\$4} END{for( i in x ){print i,x[i]}}' groupandsum.txt
10/12/2002-chemical 3
03/12/2003-Electrucal 1
03/12/2003-electrical 3
02/12/2002-Mechanical 1
10/12/2003-mechamical 2
02/12/2002-physics 2
10/11/2003-Electrucal 2
10/11/2003-chemical 2
10/12/2003-chemical 2
[root@localhost wms]#

awk -F'|' '{a[\$5]+=\$4}END{for(i in a)printf("%s\t%d\n",i,a[i])}' groupandsum.txt

[root@localhost wms]# awk -F'|' '{sum+=\$4} END {print "Average = ", sum/NR}' groupandsum.txt
Average =  2

[root@localhost wms]# awk -F'|' '{a[\$5]+=\$4;ca[\$5]++}END{for(i in a)printf("%s\t%d\taverage:%d\n",i,a[i],a[i]/ca[i])}' groupandsum.txt
03/12/2003      4       average:2
02/12/2002      3       average:1
10/12/2002      3       average:3
10/12/2003      4       average:2
10/11/2003      4       average:2

/ 也能求得完整的值，只是没有显示而已，用%.2f就可以显示了

[root@localhost wms]# awk -F'|' '{a[\$5]+=\$4;ca[\$5]++}END{for(i in a)printf("%s\t%d\t%.2f\n",i,a[i],a[i]/ca[i])}' groupandsum.txt
03/12/2003      4       2.00
02/12/2002      3       1.50
10/12/2002      3       3.00
10/12/2003      4       2.00
10/11/2003      4       2.00

[root@localhost wms]# awk -F'|' 'BEGIN {max=0}{if(\$4>max){max=\$4}} END{print max}' groupandsum.txt
3

[root@localhost wms]# awk -F'|' '{if(\$4>x[\$5]){x[\$5]=\$4}} END{for (i in x) print i,x[i] }' groupandsum.txt
03/12/2003 3
02/12/2002 2
10/12/2002 3
10/12/2003 2
10/11/2003 2

a 111
a 222
a 333
b 444
d 555

>awk '{x[\$1]=x[\$1]"\n"\$2} END{for( i in x ){print i":", x[i]}}' juhe.txt
a:
111
222
333
b:
444
555

ORACLE开发优化群：134426686 欢迎加入

• 博文量
24
• 访问量
119100