12

I have a tab-separated file, and I want to sort it by its columns 9, 14, and 16. By this I mean that all rows that agree at column 9 should be sorted by column 14, and among these, those that also agree on column 14 should be sorted by column 16. (All columns should be sorted ascendingly, but the first two should be sorted alphabetically, and the remaining one numerically.)

I've tried various forms of (gnu) sort, but I don't get the desired sort order. The man page has not helped. Can anyone tell me the right incantation for achieving the sort described above?

31
sort -t '\t' -k9,9 -k14,14 -k16,16n

(remember you need to specify where sort keys start and where they end, otherwise (as in when you use -k9 instead of -k9,9) they end at the end of the line).

  • 2
    +1 for showing -k16,16n. I couldn't figure out how to attach -n or -r to a particular column. This showed me the way :) – Robert Hickman Mar 2 '18 at 17:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.