在Linux中用AWK编程:统计词频 统计考试成绩,假设学生成绩清单如下:
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Linux中用AWK编程:统计词频 统计考试成绩,假设学生成绩清单如下:相关的知识,希望对你有一定的参考价值。
jasper: 80 82 84 84 88 92
andrea: 85 89 90 90 94 95
oellis: 89 90 92 96 96 98
mona: 70 70 77 83 85 89
john: 78 85 88 91 92 94
dunce: 60 60 61 62 64 80
请统计
(1)每个学生的平均分及等级(A、B、C、D、F)
(2)班平均成绩
(3)平均成绩高于或等于班平均的人数
(4)平均成绩低于班平均的人数
(5)每个等级的人数
jasper: 80 82 84 84 88 92
andrea: 85 89 90 90 94 95
oellis: 89 90 92 96 96 98
mona: 70 70 77 83 85 89
john: 78 85 88 91 92 94
dunce: 60 60 61 62 64 80
y:root:/tmp/ss> ./xx44
Total 6 Average 83
jasper: 80 82 84 84 88 92 average 85(B)
andrea: 85 89 90 90 94 95 average 90.5(A)
oellis: 89 90 92 96 96 98 average 93.5(A)
mona: 70 70 77 83 85 89 average 79(C)
john: 78 85 88 91 92 94 average 88(B)
dunce: 60 60 61 62 64 80 average 64.5(D)
high: 4 low: 2
A - 2
B - 2
C - 1
D - 1
y:root:/tmp/ss> cat xx44
#!/bin/bash
awk 'BEGIN
sum=0
function getmark(s)
if(s<60)
mark="F"
else if(s>=60 && s<70)
mark="D"
else if(s>=70 && s<80)
mark="C"
else if(s>=80 && s<90)
mark="B"
else if(s>=90)
mark="A"
return mark
name[NR]=$1
subtotal[$1]=0
for(i=2;i<=NF;i++)
score[$1,i]=$i
subtotal[$1]+=$i
average[$1]=subtotal[$1]/(NF-1)
sum+=average[$1]
END
allav=sum/NR
locount=0
hicount=0
printf("Total %d Average %d\\n\\n", NR, allav)
for(key in name)
printf("%s\\t", name[key])
for(i=2;i<=NF;i++)
printf("%s ", score[name[key],i])
printf("average %s(%s)\\n",
average[name[key]],
getmark(average[name[key]]))
if(average[name[key]] >= allav)
hicount++
else
locount++
markcnt[getmark(average[name[key]])]++
printf("\\n");
printf("\\nhigh: %s low: %s\\n", hicount, locount)
for(key in markcnt)
printf("%s - %s\\n", key, markcnt[key])
' xx44.dat 参考技术A 优化了一下luneng8183脚本,少了20行代码.
awk -F[:\\ ] 'BEGIN
sum=0
function getmark(s)
if(s>=90)
mark="A"
else if(s>=80 && s<90)
mark="B"
else if(s>=70 && s<80)
mark="C"
else if(s>=60 && s<70)
mark="D"
else if(s>=70 && s<60)
mark="E";
return mark
for(i=2;i<=NF;i++)
a[$1]+=$i
s+=$i;
;
peravg[$1]=a[$1]/(NF-1);
perlev[$1]=getmark(peravg[$1])
END
classavg=s/FNR;
hicount=0;
locount=0;
for(i in peravg)
printf "%-8s peravg score is %-5d - %-4s\\n",i,peravg[i],perlev[i];
if(a[i]>classavg)hicount++
else
locount++
;
markcn[getmark(peravg[i])]++
;
print "-----------------------------------"
printf "classavg %-10s\\n",classavg;
printf "hicount %-d, locount %-d\\n",hicount,locount;
for(j in markcn)
printf "%-s - %-s\\n",j,markcn[j];
print "-----------------------------------"
' 1.txt
-bash-3.2$ scoreStatics.sh
john peravg score is 88 - B
andrea peravg score is 90 - A
oellis peravg score is 93 - A
dunce peravg score is 64 - D
jasper peravg score is 85 - B
mona peravg score is 79 - C
-----------------------------------
classavg 500.5
hicount 4, locount 2
A - 2
B - 2
C - 1
D - 1
-----------------------------------
以上是关于在Linux中用AWK编程:统计词频 统计考试成绩,假设学生成绩清单如下:的主要内容,如果未能解决你的问题,请参考以下文章