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:

"In the face of entropy and nothingness, you kind of have to pretend it's not there if you want to keep writing good code." -- Karl Lehenbauer

Working...