在Linux中用AWk编程:统计词频 统计考试成绩,假设学生成绩清单如下

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Linux中用AWk编程:统计词频 统计考试成绩,假设学生成绩清单如下相关的知识,希望对你有一定的参考价值。

ojasper:80 82 84 84 88 92
andrea: 85 89 90 90 94 95
ellis: 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)每个等级的人数

(1)每个学生的总分平均分

awk \'for ( a=2;a<=(NF+1);a++ )sum=sum+$aprint $1,sum/(NF-1);sum=0\' /test/student.data | sort -k2 -r | awk \'BEGINA[1]="A";A[2]="B";A[3]="C";A[4]="D";A[5]="E";A[6]="F"print A[NR]":",$0\'
(2)每个科目的平均分(从上至下平均分分别是数据中从左至右科目的)

#!/bin/bash
for (( i=2;i<=(NR+1);i++ ))
do
awk \'sum+=$\'$i\'ENDprint sum/NR\' /test/student.data
done
(2)-1班级总平均分
awk \'for ( a=2;a<=(NF+1);a++ )sum=sum+$aENDprint sum/(NF-1)\' student.data
参考技术A #!/bin/awk -f
BEGIN
FS="[: ]"
#保存学生数
count=0;
great=0;
less=0;



#student_average用于保存每个学生的平均分
student_average[$1]=($2+$3+$4+$5+$6+$7)/6


END
#class_average保存班平均分
for (student in student_average)

print student": "student_average[student]
count++;
class_average+=student_average[student]*6

class_average/=count;
print "class average: "class_average

for(student in student_average)

if(student_average[student]*6>=class_average)
great++;
else
less++;


print "the count of student that greater than average: "great
print "the count of student that less than average: "less


我没写等级相关的统计,因为你没给出每个的等级的分数线

如何运行:
把代码保存为student.awk
chmod +x student.awk
./sutdent.awk student.data
student.data是你的数据文件

组合数据类型练习,英文词频统计实例上

 

1 字典实例:建立学生学号成绩字典,做增删改查遍历操作

a={\'01\':100,\'02\':89,\'03\':88,\'04\':90}
print(\'字典\',a)
a[\'05\']=88
print(\'增加05学生成绩为88\',a)
a.pop(\'02\')
print(\'删除02的学生成绩\',a)

print(\'查找03的学生成绩\',a.get(\'03\'))
print(\'查找09的学生成绩\',a.get(\'09\',\'没有该学生\'))
a[\'04\']=77
print(\'修改04的学生成绩为77\',a)

2 列表,元组,字典,集合的遍历。

jian=(\'0123456\')
zhi=(\'abcdefg\')
biao=list(\'abcdefg\')
jihe=set(biao)
yuan=tuple(\'abcdefg\')
b=dict(zip(jian,zhi))
print(b)
print(\'打印列表\')
for y in biao:
    print(y)
print(\'打印字典\')
for x in b:
     print(x,b[x])
print(\'打印元祖\')
for y in yuan:
    print(y)
print(\'打印集合\')
for y in jihe:
    print(y)

总结列表,元组,字典,集合的联系与区别

 列表和元组是有序的,字典和集合都是无序的。

列表用“[]”表示。列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。

元组用“()”表示。元组是只读的,不能修改。元组一旦定义其长度和内容都是固定的。

字典用“{}”表示。字典存储键值对(key-value)数据。每一组用冒号连起来,然后各组用逗号隔开。

集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合内的元素没有重复的元素,主要是消除重复元素。

3 英文词频统计实例

str=\'\'\'Ohh wooaah  
You know you love me,I know you care
You shout whenever, And I\'ll be there
You want my love, You want my heart
And we will never ever ever be apart
Are we an item? Girl quit playing
Were just friends, Or are we saying
So theres another one, Looks right in my eyes
My first love broke my heart for the first time,
And I was like
Baby, baby, baby nooo
My baby, baby, baby noo
My baby, baby, baby nooo
I thought youd always be mine mine
Baby, baby, baby nooo
My baby, baby, baby noo
My baby, baby, baby nooo
I thought youd always be mine, oh oh
For you, I would have done whatever
Another chance and we, We get together
And wanna play it cool, About loosin you
I\'ll buy you anything, I buy you any ring
And i in piece , baby fix me
And you shake me til you wake me from this bad dream
Im going down, down, dooown
And just can\'t believe my first love won be around
Baby, baby, baby nooo
Baby, baby, baby nooo
My baby, baby, baby noo
My baby, baby, baby nooo
I thought youd always be mine
When i was 13 i had my first love
Here was nobody to compare my baby
And nobody came between us or could ever come above
She had me goin crazy
Oh i was starstruck.
She woke me up daily dont need no starbucks
She made my heart pound
Asking for a beat when i see her in the street
And in the school on the playground
But i really wanna see her on the weekends
She knows she got me dazy
Cause she was so amazing and now my heart is breaking
But i just keep on sayin
Baby, baby, baby nooo
My baby, baby, baby noo
My baby, baby, baby nooo
I thought youd always be mine  
Now Im all gone\'\'\'
print(\'待分析字符串\',str)

for i in \',.?!\\n\':
    str=str.replace(i,\' \')
str=str.lower()
str=str.split(" ")
print(\'单词计数词典\')

words=set(str)

dt={}
dt[\'baby\']=str.count(\'baby\')
dt[\'love\']=str.count(\'love\')
dt[\'my\']=str.count(\'my\')
dt[\'me\']=str.count(\'me\')
for j in words:
    dt[j]=str.count(j)
for j in dt:
    print("{0:<11}{1}".format(j,dt[j]))
                     


 

 

 

以上是关于在Linux中用AWk编程:统计词频 统计考试成绩,假设学生成绩清单如下的主要内容,如果未能解决你的问题,请参考以下文章

excel中用到的函数

组合数据类型练习,英文词频统计实例上

组合数据类型练习,英文词频统计实例上

组合数据类型练习,英文词频统计实例上

组合数据类型练习,英文词频统计实例上

Linux Shell编程实战---统计特定文件中单词的词频