Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
GNU is Not Unix

Journal Synonymous Dastard's 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

This discussion has been archived. No new comments can be posted.

Using sort

Comments Filter:

You must realize that the computer has it in for you. The irrefutable proof of this is that the computer always does what you tell it to do.

Working...