Intermediate · 25 min · practise every command in the built-in terminal

🛠 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 -l counts the header too. A CSV almost always starts with one, so skip it with NR > 1 or tail -n +2.

Practice tasks

  1. How many sales are in ~/data/sales.csv? (The header isn't a sale.)
  2. How much money came in? Add up quantity × price over all the sales
  3. Revenue per product, the best seller first
  4. Revenue per month: September, then October
  5. Write ~/scripts/sales.sh: it saves the products (best first) and the total, with two decimals, into ~/reports/sales.txt

Commands in this lesson

Projects