🛠 Project: Sales Report from a CSV
The café's till exports every sale into ~/data/sales.csv. The owner wants a report: what sold best, how much came in in total, and how each month went. No spreadsheet needed, because awk adds up columns.
date,product,quantity,price
2026-09-01,Cappuccino,6,3.50
With -F, awk splits on commas: $1 date, $2 product, $3 quantity, $4 price. Revenue is $3 * $4.
Building blocks
tail -n +2 ~/data/sales.csv # Everything but the header line
awk -F, 'NR > 1 {print $2}' ~/data/sales.csv # NR > 1: skip line 1
awk -F, 'NR > 1 {t += $3 * $4} END {print t}' ~/data/sales.csv # A running total, printed at the END
awk -F, 'NR > 1 {r[$2] += $3 * $4} END {for (p in r) print p, r[p]}' ~/data/sales.csv # One total per product
sort -k2 -rn # By the 2nd column, as numbers, highest first
substr($1, 1, 7) # 2026-09-01 → 2026-09 (the month)
printf "%-12s %8.2f\n", p, r[p] # In awk: name left, number right, 2 decimals
The report
~/scripts/sales.sh writes ~/reports/sales.txt:
Cappuccino 140.00
Espresso 80.00
…
Total 362.00
💡
wc -lcounts the header too. A CSV almost always starts with one, so skip it withNR > 1ortail -n +2.