Awk Field Aggregate And Report
Use awk to aggregate fields by key with conditional logic and formatted output for log analysis.
Code
#!/usr/bin/env bash
set -euo pipefail
awk '
{
key = $1
value = $2
sum[key] += value
count[key]++
}
END {
for (k in sum) {
printf "%-20s total=%10d count=%6d avg=%.2f\n", \
k, sum[k], count[k], sum[k] / count[k]
}
}
' access.log | sortReport a problem with this exampleOpens a pre-filled issue on GitHub.