Using some tools that actually parse the CSV
The original question asked about using sort specifically, but since CSV files are not parseable by sort in general, here are some options that actually correctly parse your CSV to help other Googlers.
I'll test them with these particularly hard to parse CSV files containing a string with both a comma and a newline:
cat >noheader.csv <<EOF
12,"xx,
yy",4
12,aa,3
2,bb,1
2,cc,2
EOF
and:
cat >header.csv <<EOF
first,second,third
12,"xx,
yy",4
12,aa,3
2,bb,1
2,cc,2
EOF
and I'll sort the CSV by first and third column treating values as integers which should give:
2,bb,1
2,cc,2
12,aa,3
12,"xx,
yy",4
Miller (Go CLI tool)
Install Miller 6.12.0 on Ubuntu 24.10:
sudo apt install miller
With headers we can use:
mlr --csv sort -n first -n third header.csv
or as an equivalent shortcut:
mlr --csv sort -n first,third header.csv
both of which output:
first,second,third
2,bb,1
2,cc,2
12,aa,3
12,"xx,
yy",4
-n means treat column as numerical rather than lexicographic sort.
If the CSV does not have headers, we can generate headers named 1, 2, ... with --implicit-csv-header:
mlr --implicit-csv-header --csv sort -n 1,3 noheader.csv
which outputs:
1,2,3
2,bb,1
2,cc,2
12,aa,3
12,"xx,
yy",4
You might also want to remove the header from output with --headerless-csv-output:
mlr --headerless-csv-output --implicit-csv-header --csv sort -n 1,3 noheader.csv
and then it outputs just:
2,bb,1
2,cc,2
12,aa,3
12,"xx,
yy",4
Source code at: https://github.com/johnkerl/miller
Related documentation:
Sweet CLI tool with all that we need!
csvsort from csvtool (Python)
They don't have proper numerical sort: https://github.com/wireservice/csvkit/issues/151 so I'm not even going to bother for now.
Related
sortis an unreliable foundation in the real world where quotation happens to be