我有一个具有以下结构的文件: student_name subject1 grade1 subject2 grade2 ...
我想出了如何计算每个科目有多少才能参加,但我不知道如何计算每个科目,其中有多少人的成绩大于5。
此外,我需要帮助找到每个科目的成绩,以及哪个成绩最高。
如果有人可以帮助我,我会很感激!
File Content: Robert Java 8 Algorithms 8 Math 6 Andrew Java 9 Algorithms 7 Math 6 Ben Java 5 Algorithms 10 Math 8 Jerry Java 7 Algorithms 5 Math 7 Terry Java 4 Algorithms 5 Math 9 Ross Java 5 Algorithms 9 Math 7 Desired output: The subject Java was attended by 6 stundets. Number of students with grade bigger than or equal to 5: 5 The subject Algorithms was attended by 6 students. Number of students with grade bigger than or equal to 5: 6 The subject Math was attended by 6 students. Number of students with grade bigger than or equal to 5: 6 Occurance of grades: 5: 6: 7: 8: 9: Occurance of grades for Math: 6: 7: 8: 9:
到目前为止我得到了什么:
awk ' { for (i = 2; i <= NF; i += 2) C[$i]++;} {for (j = 3; j <= NF; j += 2) B[$j]++;} END { for (var in C) print "The ",var," subject was attanded by " C[var], "students" ; for (var3 in B) print "The occurance of grade: ",var3," is : ", B[var3]; }'\n $1
答案 0 :(得分:0)
这是一个可用于添加打印格式的模板......
$ awk '{for(i=2;i<NF;i+=2)
{k=$i; g=$(i+1); a[k]++;
c[k]+=(g>=5);
grades[k]=(k in grades?grades[k]:k) ORS g}}
END{for(k in a) print k, a[k], c[k];
for(k in grades) print grades[k]}' file
Algorithms 6 6
Math 6 6
Java 6 5
Algorithms
8
7
10
5
5
9
Math
6
6
8
7
9
7
Java
8
9
5
7
4
5
a
数组包含主题的计数,c
表示符合>=5
条件的数据; grades
具有所有科目的成绩。在END
块,您遍历数组/映射以选择存储的值。请注意您的#&#34; Java&#34;在发布的示例中,计数不正确。