CODE: [root@localhost tmp]# cat classavg.txt Name Chinese Math C Mysql Me 97 99 87 78 li 87 90 64 49 wang 68 73 59 92 zhang 93 49 92 58 zhou 60 99 88 50 |
CODE: [root@localhost tmp]# sh avg.sh classavg.txt Name Chinese Math C Mysql TOTAL_stu AVG_stu Me 97 99 87 78 361 90.25 li 87 90 64 49 290 72.5 wang 68 73 59 92 292 73 zhang 93 49 92 58 292 73 zhou 60 99 88 50 297 74.25 =============================================================== AVG_cls 81 82 78 65.4 # # AVG>= 3 3 3 2 AVG< 2 2 2 3 [root@localhost tmp]# |
CODE: #!/bin/bash awk ' NR==1{print $0"\tTOTAL_stu\tAVG_stu" next } {TOTAL_stu=0;for(i=2;i<=NF;i++) TOTAL_stu+=$i AVG_stu=TOTAL_stu/(NF-1) printf("%s\t%g\t\t%g\n",$0,TOTAL_stu,AVG_stu) AVG_F2[NR] = $2 AVG_F3[NR] = $3 AVG_F4[NR] = $4 AVG_F5[NR] = $5 AVG_CN_total += $2 AVG_Math_total += $3 AVG_C_total += $4 AVG_Mysql_total += $5 } END{ AVG_CN = AVG_CN_total/(NR-1) AVG_Math = AVG_Math_total/(NR-1) AVG_C = AVG_C_total/(NR-1) AVG_Mysql = AVG_Mysql_total/(NR-1) for(i=2;i<=NR;i++) { if (AVG_F2[i]>=AVG_CN) ++AVG_above_CN else ++AVG_below_CN if (AVG_F3[i]>=AVG_Math) ++AVG_above_Math else ++AVG_below_Math if (AVG_F4[i]>=AVG_C) ++AVG_above_C else ++AVG_below_C if (AVG_F5[i]>=AVG_Mysql) ++AVG_above_Mysql else ++AVG_below_Mysql } print "===============================================================" OFS="\t";print "AVG_cls",AVG_CN,AVG_Math,AVG_C,AVG_Mysql,"#","\t""#" OFS="\t";print "AVG>=",AVG_above_CN,AVG_above_Math,AVG_above_C,AVG_above_Mysql OFS="\t";print "AVG<",AVG_below_CN,AVG_below_Math,AVG_below_C,AVG_below_Mysql } ' $* |
[火星人 ] 自編自解一道awk題已經有546次圍觀