在Linux中用AWK编程:统计词频 统计考试成绩,假设学生成绩清单如下: 20
jasper:808284848892andrea:858990909495oellis:899092969698mona:707077838589john:788588...
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)每个等级的人数 展开
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)每个等级的人数 展开
展开全部
y:root:/tmp/ss> cat xx44.dat
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
展开全部
优化了一下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
-----------------------------------
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询