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.


1 Answer 1


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .