Can you sort one column lexicographically while simultaneously sorting the next column numerically?

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.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply