Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror
GNU is Not Unix

Journal Journal: Using sort

Damn sort, not working the way I thought.

I post this mainly as a self-reminder, but it might help others.

sort -n

is just not enough to numerically sort several columns.

For example:

$ (echo "12 12"; echo "2 12"; echo "2 2"; echo "12 2") | sort -n
2 12
2 2
12 12
12 2

The first column is correctly sorted, but not the second one.

One solution (I don't know if this is the best one) is adding -k1 -k2. It works but it means that you have to know how many columns there are.

$ (echo "12 12"; echo "2 12"; echo "2 2"; echo "12 2") | sort -n -k1 -k2
2 2
2 12
12 2
12 12

Slashdot Top Deals

Riches: A gift from Heaven signifying, "This is my beloved son, in whom I am well pleased." -- John D. Rockefeller, (slander by Ambrose Bierce)

Working...