Interview Prep/Real-World Interview/awk, sed, grep and jq one-liners

Print the average, min and max of a numeric column with awk.

IntermediateHands-onawk, sed, grep and jq one-liners
Answer
awk 'NR==1 {min=max=$1} {s+=$1; if ($1<min) min=$1; if ($1>max) max=$1}
     END {printf "avg=%.2f min=%s max=%s n=%d\n", s/NR, min, max, NR}' latencies.txt

Key trick → Seed min and max from the first row. Starting at 0 gives a wrong min for all-positive data.

All Real-World Interview questions Roadmap: Interview readiness

More on awk, sed, grep and jq one-liners