Sales Report with awk
The sales team wants numbers from sales.csv (columns: date, region, product, amount). Use awk, with sort and friends where they help.
How it works: the terminal below is a real Linux machine running in your browser. Click it and
type. Tasks tick themselves off as you complete them; questions are answered in the box under each task.
Press Reset any time for a fresh machine. Your password on this machine (for
sudo) is student.
Hints
Print the columns you're working with first, for example awk -F, '{print $2, $4}' sales.csv, to check you have the right ones.
Solution explanation
-F, splits on commas, so $2 is the region and $4 the amount. Totals are added up line by line and printed in END. An array (s[$2]) keeps one total per region; awk prints arrays in no particular order, so sort puts the regions in order. For the biggest sale, sorting by the 4th column as a number puts it last.
Show solution
awk -F, 'NR > 1 {t += $4} END {print t}' sales.csv
awk -F, '$2 == "North" {t += $4} END {print t}' sales.csv
awk -F, 'NR > 1 && $4 > 500' sales.csv | wc -l
tail -n +2 sales.csv | sort -t, -k4 -n | tail -n 1
awk -F, 'NR > 1 {s[$2] += $4} END {for (r in s) print r, s[r]}' sales.csv | sort > by-region.txt