Bash

Sum a Column with awk

admin by @admin ADMIN
8m ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Awk's default field splitter is whitespace; pass a single character with -F. Perfect for summing the Nth column of a log file or CSV.
Bash
Raw
# Sum the request-byte column (col 10) from an nginx access log
awk '{ s += $10 } END { print s }' access.log

# Sum a column from a CSV (-F sets the field separator)
awk -F, '{ s += $3 } END { print s }' transactions.csv

# Average instead of sum
awk '{ s += $1; n++ } END { print s/n }' numbers.txt

# Sum grouped by column 2 (like SQL GROUP BY)
awk -F, '{ by[$2] += $3 } END { for (k in by) print k, by[k] }' transactions.csv | sort -k2 -rn
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.