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
  1. 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
提供一种方法
  1. [root@study study]# cat file1
  2. chinese:85
  3. chinese:92
  4. chinese:79
  5. chinese:86
  6. chinese:81
  7. chinese:76
  8. chinese:84
  9. chinese:84
  10. chinese:88
  11. chinese:78
  12. math:75
  13. math:84
  14. math:83
  15. math:79
  16. math:85
  17. math:90
  18. math:94
  19. math:84
  20. math:69
  21. math:80
  22. english:92
  23. english:76
  24. english:83
  25. english:81
  26. english:87
  27. english:83
  28. english:82
  29. english:79
  30. english:79
  31. physics:68
  32. physics:75
  33. physics:69
  34. physics:90
  35. physics:83
  36. physics:83
  37. physics:82
  38. physics:79
  39. physics:69
  40. physics:85
  41. chemistry:80
  42. chemistry:76
  43. chemistry:77
  44. chemistry:83
  45. chemistry:83
  46. chemistry:82
  47. chemistry:90
  48. chemistry:87
  49. chemistry:88
  50. chemistry:90
  51. politics:93
  52. politics:89
  53. politics:90
  54. politics:92
  55. politics:99
  56. politics:92
  57. politics:89
  58. politics:90
  59. politics:91
  60. politics:91
  61. [root@study study]# cat file1 | awk -F: '{print $1}' | uniq > file2
  62. [root@study study]# cat file2
  63. chinese
  64. math
  65. english
  66. physics
  67. chemistry
  68. politics
  69. [root@study study]#
  70. [root@study study]#
  71. [root@study study]# sh  script1.sh     
  72. 833
  73. 823
  74. 742
  75. 783
  76. 836
  77. 916
  78. [root@study study]# cat script1.sh
  79. #!/bin/sh
  80. for i in `cat file2`
  81. do
  82.   awk -F: '(var==$1){total+=$2;}END{print total}' var=$i file1
  83. done
复制代码

作者: moperyblue    时间: 2016-04-28 00:29
  1. awk -F: '$1!=pre{
  2.     n+=1
  3. }
  4. {
  5.     a[n][$1]+=$2
  6. }
  7. {
  8.     pre=$1
  9. }
  10. END{
  11.     for(i=1;i<=n;i++){
  12.         for(j in a[i]){
  13.             print j":"a[i][j]
  14.         }
  15.     }
  16. }' file #gawk 4.0+
复制代码

作者: 99超人    时间: 2016-04-28 08:52
提示: 作者被禁止或删除 内容自动屏蔽
作者: mswsg    时间: 2016-04-28 09:30
本帖最后由 mswsg 于 2016-04-28 09:34 编辑
  1. with open('1.txt', 'r') as f:
  2.     alist = []
  3.     d = {}
  4.     lines = f.readlines()
  5.     for line in lines:
  6.         line = line.strip().split(':')
  7.         alist.append(line)

  8.     for i in alist:
  9.         try:
  10.             d[i[0]] += int(i[1])
  11.         except KeyError:
  12.             d[i[0]] = int(i[1])
  13.     for course, score in d.items():
  14.         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
  1. 不排序:
  2. awk -F":" '{a[$1]+=$2}END{for(i in a)print i":"a[i]}' file
  3. politics:916
  4. english:742
  5. chinese:833
  6. chemistry:836
  7. physics:783
  8. math:823

  9. 按考试科目排序:
  10. awk -F":" '{a[$1]+=$2}END{s=asorti(a,t);for(i=1;i<=s;i++)print t[i]":"a[t[i]]}' file
  11. chemistry:836
  12. chinese:833
  13. english:742
  14. math:823
  15. physics:783
  16. politics:916
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2