Sample output
East,Widgets,94200,Q3
West,Widgets,87500,Q2
East,Gadgets,76300,Q1
The third column is revenue, and the output is sorted from highest to lowest.
When you would reach for it
A quick look at a CSV export before deciding whether it is worth importing into a database or opening in a spreadsheet. Also useful in pipelines that feed into other commands.
Gotchas
- If any field contains a comma inside quotes,
sort -t,will split on the wrong boundary. For serious CSV parsing, usecsvtoolormlr(Miller). - Headers will sort into the output. Pipe through
tail -n +2first to skip the header, or usehead -1 file; tail -n +2 file | sort ...to preserve it.
Variants
$ sort -t$'\t' -k3 -nr data.tsv | head -20
Tab-separated files. Use $'\t' as the delimiter
$ mlr --csv sort-by -nr revenue sales.csv | head -20
Miller. Handles quoted fields and headers correctly