Chinaunix
标题:
汇总计算第二列
[打印本页]
作者:
oldknew
时间:
2016-04-27 19:58
标题:
汇总计算第二列
假设一文本为一考生一学期参加的所有考试的成绩:
[aii@localhost gjy]$ cat test.txt
chinese:85
chinese:92
chinese:79
chinese:86
chinese:81
chinese:76
chinese:84
chinese:84
chinese:88
chinese:78
math:75
math:84
math:83
math:79
math:85
math:90
math:94
math:84
math:69
math:80
english:92
english:76
english:83
english:81
english:87
english:83
english:82
english:79
english:79
physics:68
physics:75
physics:69
physics:90
physics:83
physics:83
physics:82
physics:79
physics:69
physics:85
chemistry:80
chemistry:76
chemistry:77
chemistry:83
chemistry:83
chemistry:82
chemistry:90
chemistry:87
chemistry:88
chemistry:90
politics:93
politics:89
politics:90
politics:92
politics:99
politics:92
politics:89
politics:90
politics:91
politics:91
怎样写脚本或命令行算出该考生各门科目在这个学期的总成绩?
返回:
[aii@localhost gjy]$ cat test.txt
chinese:833
math:823
english:825
physics:783
chemistry:836
politics:916
english:83
作者:
haooooaaa
时间:
2016-04-27 20:00
awk -F: '{a[$1]+=$2}END{for(i in a)print i":"a[i]}' file
复制代码
作者:
oldknew
时间:
2016-04-27 20:14
非常感谢。
作者:
zy86416779
时间:
2016-04-27 22:27
回复
1#
oldknew
提供一种方法
[root@study study]# cat file1
chinese:85
chinese:92
chinese:79
chinese:86
chinese:81
chinese:76
chinese:84
chinese:84
chinese:88
chinese:78
math:75
math:84
math:83
math:79
math:85
math:90
math:94
math:84
math:69
math:80
english:92
english:76
english:83
english:81
english:87
english:83
english:82
english:79
english:79
physics:68
physics:75
physics:69
physics:90
physics:83
physics:83
physics:82
physics:79
physics:69
physics:85
chemistry:80
chemistry:76
chemistry:77
chemistry:83
chemistry:83
chemistry:82
chemistry:90
chemistry:87
chemistry:88
chemistry:90
politics:93
politics:89
politics:90
politics:92
politics:99
politics:92
politics:89
politics:90
politics:91
politics:91
[root@study study]# cat file1 | awk -F: '{print $1}' | uniq > file2
[root@study study]# cat file2
chinese
math
english
physics
chemistry
politics
[root@study study]#
[root@study study]#
[root@study study]# sh script1.sh
833
823
742
783
836
916
[root@study study]# cat script1.sh
#!/bin/sh
for i in `cat file2`
do
awk -F: '(var==$1){total+=$2;}END{print total}' var=$i file1
done
复制代码
作者:
moperyblue
时间:
2016-04-28 00:29
awk -F: '$1!=pre{
n+=1
}
{
a[n][$1]+=$2
}
{
pre=$1
}
END{
for(i=1;i<=n;i++){
for(j in a[i]){
print j":"a[i][j]
}
}
}' file #gawk 4.0+
复制代码
作者:
99超人
时间:
2016-04-28 08:52
提示:
作者被禁止或删除 内容自动屏蔽
作者:
mswsg
时间:
2016-04-28 09:30
本帖最后由 mswsg 于 2016-04-28 09:34 编辑
with open('1.txt', 'r') as f:
alist = []
d = {}
lines = f.readlines()
for line in lines:
line = line.strip().split(':')
alist.append(line)
for i in alist:
try:
d[i[0]] += int(i[1])
except KeyError:
d[i[0]] = int(i[1])
for course, score in d.items():
print course, score
复制代码
作者:
mswsg
时间:
2016-04-28 09:31
english 的和求错了?
chinese 833
english 742
politics 916
chemistry 836
physics 783
math 823
回复
1#
oldknew
作者:
tolilong
时间:
2016-04-28 09:41
awk -F":" '{a[$1]+=$2}END{for(i in a){print i":"a[i]}}' filename
作者:
jcdiy0601
时间:
2016-04-28 09:44
awk 'BEGIN{FS=OFS=":"}{a[$1]+=$2}END{for(i in a)print i,a[i]}' file
english:742
physics:783
chinese:833
politics:916
chemistry:836
math:823
作者:
oldknew
时间:
2016-04-30 11:08
不好意思,求错了哈
作者:
wh7211
时间:
2016-04-30 14:20
本帖最后由 wh7211 于 2016-04-30 14:22 编辑
回复
1#
oldknew
不排序:
awk -F":" '{a[$1]+=$2}END{for(i in a)print i":"a[i]}' file
politics:916
english:742
chinese:833
chemistry:836
physics:783
math:823
按考试科目排序:
awk -F":" '{a[$1]+=$2}END{s=asorti(a,t);for(i=1;i<=s;i++)print t[i]":"a[t[i]]}' file
chemistry:836
chinese:833
english:742
math:823
physics:783
politics:916
复制代码
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2