Plainnotes

Summing and grouping columns with awk

2025-10-22 · 2 min readTools

A log has a response-time column; you want per-endpoint averages without writing Python.

Sum:

awk '{sum+=$NF; n++} END {printf "%.2f\n", sum/n}' access.log

Group:

awk '{c[$7]++; t[$7]+=$NF} END {for (k in c) printf "%-20s %6d %8.1f\n", k, c[k], t[k]/c[k]}' access.log

awk's associative arrays are unordered, so pipe through sort -k2 -nr. On large files LC_ALL=C speeds things up by skipping UTF-8 validation.

Percentiles are awkward in awk; storing values and sorting is easier:

awk '{print $NF}' access.log | sort -n | awk '{a[NR]=$1} END {print a[int(NR*0.99)]}'