No, -k1,2
says to sort on the portion of the line that starts at the beginning of the first field and ends at the end of the second field.
To sort on the first field and then on the second, it's:
sort -k1,1 -k2,2
CSV is a structured document format. As such, simple text manipulation tools like cut
(or sort
, sed
, or awk
, unless the data is simple) are inadequate for processing CSV files safely and conveniently (because fields may contain embedded delimiters and newlines). Instead, it would be best if you were using a CSV-aware processing tool such as Miller (mlr
).
The following Miller command parses the file as a header-less CSV file, sorting it numerically ascending by its 12th field:
mlr --csv -N sort -n 12 file
If you have headers in your CSV data, drop the -N
option and use the header name in place of 12
, e.g.,
mlr --cvs sort -n pvalue file
To extract column 12,
mlr --csv -N cut -f 12 file
To sort and cut, and also only get the 10 first results,
mlr --csv -N sort -n 12 then cut -f 12 then head -n 10 file
Again, drop the -N
and use the field names if you have headers in the input.
With the csvkit toolkit, you could use csvsort
to get the same result like so:
csvsort -H -c 12 file | tail -n +2
(the tail
command removes the headers that csvsort
generates), or, with headers in the input,
csvsort -c pvalue file
Extracting individual fields with csvcut
:
csvcut -H -c 12 file
Combined with csvsort
:
csvsort -H -c 12 file | csvcut -c 12 | head -n +2
Or, with headers,
csvsort -c pvalue file | csvcut -c pvalue
There is no csvhead
command, so limiting the resutl to 10 records will have to be doen some other way, possibly through mlr --csv head -n 10
.
Best Answer
Sorting columns Individually: