#! /usr/bin/gawk -f # Last edited on 2024-01-01 10:29:45 by stolfi # This file must be in the iso-latin-1 encoding. # Reads from {stdin} the {catsep} output of bunch of lab grade files, # one for each day. Writes to stdout a consolidated file of grades, with # averages. Each file should be called "{YYYY}-{MM}-{DD}-lab.txt" The # first three columns in each file should be "|", the RA, and the lab grade # for the day. Blank and comment lines are ignored. # Needs "-f mc937_read_alunos.gawk" to read the file "alunos.txt". BEGIN { ngr = 0 # Number of grades included. nra = 0 # Number of distinct RAs seen split("", grs) # Element {grs[ra,igr]} is grade indexed {igr} of student {ra}. split("", dts) # Element {dts[igr]} is the date of grade indexed {igr}. split("", ras) # Element {ras[ira]} is RA indexed {ira}. split("", iras) # Element {iras[ra]} is the index of RA {ra} in {ras}. split("", nam) # Element {nam[ra]} is the name of student with RA {ra}. split("", niv) # Element {niv[ra]} is the level ("GD", "EP", ...) of student with RA {ra}. read_alunos("alunos.txt", nam, niv); } /^[#]FILE / { fname = $2 dt = fname gsub(/-lab.txt/, "", dt) gsub(/^.*[\/]/, "", dt) igr = ngr dts[igr] = dt ngr++ next } //{ gsub(/[ ]*[#].*$/, "", $0) } /^ *$/ {next} /^[-+ ]*$/ {next} /^ *[~]+ *$/ {next} /^ *[|] *[0-9][0-9][0-9][0-9][0-9][0-9] ([0-9][0-9][0-9]|---)/ { ra = $2 va = $3 if (ra in iras) { ira = iras[ra] } else { ira = nra ras[ira] = ra iras[ra] = ira nra++ } gr[ra,igr] = va next } // { data_error("invalid line format") } END { printf "#\n" printf "# Notas e média de laboratórios\n" printf "# A média foi calculada excluindo as duas menores notas\n" printf "\n" printf "# RA " for (igr = 0; igr < ngr; igr++) { printf " T%02d", igr } printf " MED" printf " Nome" printf "\n" print_dashes() # Print max score pseudo-student printf "| %s", "999999" for (igr = 0; igr < ngr; igr++) { printf " %s", "100" } printf " %03d", 100 printf " %s", "Max_von_Genius" printf "\n" print_dashes() # Print scores of real students: for (ira = 0; ira < nra; ira++) { ra = ras[ira] printf "| %s", ra # Compute average grade {med} and number of hand-ins {nhi}: nhi = 0 tot = 0 sma1 = 999 # Smallest grade of student. sma2 = 999 # Second-smallest grade of student. for (igr = 0; igr < ngr; igr++) { va = gr[ra,igr] if (va == "") { va = "---" } if (va != "---") { tot += va } printf " %s", va # Keep track of 2 smallest: if (va == "---") { nva = 0 } else { nva = va + 0; nhi++ } if (nva < sma1) { sma2 = sma1; sma1 = nva } else if (nva < sma2) { sma2 = nva } } med = int((tot - sma1 - sma2)/(ngr - 2) + 0.5) if (nhi > 0) { printf " %03d", med } else { printf " ---" } printf " %s", nam[ra] printf "\n" } print_dashes(); printf "\n" printf "# Datas dos laboratórios:\n" printf "\n" for (igr = 0; igr < ngr; igr++) { printf "# T%02d %s\n", igr, dts[igr] } } function print_dashes( igr) { printf "+ %s", "------" for (igr = 0; igr < ngr; igr++) { printf " %s", "---" } printf " %s", "---" printf " %s\n", "----------------------------------------" } function data_error(msg) { printf "%s:%d: ** %s\n", fname, FNR, msg > "/dev/stderr" printf " [%s]\n", $0 > "/dev/stderr" exit(1) } function table_error(fname, nlin, msg) { printf "%s:%d: ** %s\n", fname, nlin, msg > "/dev/stderr" printf " [%s]\n", $0 > "/dev/stderr" exit(1) } function arg_error(msg) { printf "** %s\n", msg > "/dev/stderr" exit(1) }