Ну, вы можете сделать это с awk
, но это не тривиально.Как это:
# We will save every course name in an array for the report, but first remove the
# the unwanted space from it's name. This line only works on the COURSE NAME lines
/^COURSE NAME/ {cn=gensub(" ","_","g",gensub(".*: ","","g",$0)); crss[cn]+=1 }
# On lines which starts with a number (student id), we are saving the student ids
# in an array (stdnts) and the students score with a "semi" multideminsional array/hash
# where the indecies are "student_id-course_name"
/^[0-9]/ {stdnts[$1]+=1 ; v[$1 "-" cn]=$2}
# after the above processing (e.g. the last line of the input file)
END {
# print the header, it's dynamic and uses the course names it saved earlier
printf("%-20s","STUDENT ID");
for (e in crss) {printf("%-20s",e)}
printf("%-20s\n","GPA")
# print the report
for (s in stdnts)
# we need to print every student id
{ printf("%-20s",s)
for (cs in crss) {
# then check if she had any score for every score, and print it
if (v[s "-" cs] > 0) {
printf("%-20s",v[s "-" cs])
}
else {
printf("%-20s","-")
}
}
printf("%-20s\n"," ")
}
}
Смотрите здесь в действии: https://ideone.com/AgaS8
Примечание :
- скрипт неоптимизировано;
- заменяет исходные названия курсов на без пробелов
- вывод таблицы студентов не отсортирован по идентификатору студента
- , требуется только первыйфайл в качестве ввода !Поместите вышеупомянутое в файл как
report.awk
, затем сделайте awk -f report.awk INPUT_FILE
.
HTH