3

I've got two columns of data I need to sort: the first column(A) needs to be sorted lexicographically and for any rows which then contain the same column A string, I need them to be sorted numerically according to what's in the second column(B).

I was thinking 'sort -f' , but that would make a '12' in column B come before a '2'.

Edit: Accidentally typed column in place of row.

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
Anj
  • 33
  • 4
  • Possible duplicate of [Trying to sort on two fields, second then first](http://unix.stackexchange.com/questions/52762/trying-to-sort-on-two-fields-second-then-first) – Alexander Mar 31 '17 at 06:59

1 Answers1

6

Yes, using the -k option to define sort keys, and the n option to specify numerical sorts:

$ echo -e "a 13\nb 2\na 2" | sort -k1,1 -k2,2n
a 2
a 13
b 2
Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164