How to Sort Floating Numbers Using the Sort Command?

Hi to all.

I'm trying to sort this with the Unix command sort.

user1:12345678:3.5:2.5:8:1:2:3
user2:12345679:4.5:3.5:8:1:3:2
user3:12345687:5.5:2.5:6:1:3:2
user4:12345670:5.5:2.5:5:3:2:1
user5:12345671:2.5:5.5:7:2:3:1

I need to get this:

user3:12345687:5.5:2.5:6:1:3:2
user4:12345670:5.5:2.5:5:3:2:1
user2:12345679:4.5:3.5:8:1:3:2
user1:12345678:3.5:2.5:8:1:2:3
user5:12345671:2.5:5.5:7:2:3:1

I want to sort it at first by the third row, at second by the fourth row and at least by the fifth row (using ":" as separator). To do it I'm trying to use this (but it doesn't works):

cat file_contains_example | sort -nr -t":" -k3 -k4 -k5

What can't I do to solve this problem? (I prefer to use sort)

Thank's for read.

-k 3 means that field is from 3th to the end of line. 3,3 means only field 3.

cat file | sort  -t :  -k 3,3nr -k 4,4nr -k 5,5nr

Hi kshji and thanks for asking so fast.

Sorry but I tried your code and don't sort the floating numbers.

Any other suggestions?

Works fine for me.
How does it sort in your case ?

Here's the test -

$ 
$ 
$ cat -n f9
     1    user1:12345678:3.5:2.5:8:1:2:3
     2    user2:12345679:4.5:3.5:8:1:3:2
     3    user3:12345687:5.5:2.5:6:1:3:2
     4    user4:12345670:5.5:2.5:5:3:2:1
     5    user5:12345671:2.5:5.5:7:2:3:1
$ 
$ sort  -t : -k 3,3nr -k 4,4nr -k 5,5nr f9
user3:12345687:5.5:2.5:6:1:3:2
user4:12345670:5.5:2.5:5:3:2:1
user2:12345679:4.5:3.5:8:1:3:2
user1:12345678:3.5:2.5:8:1:2:3
user5:12345671:2.5:5.5:7:2:3:1
$ 
$ 

What's your OS and sort version ? Do you have GNU sort ?

tyler_durden

Hi durden_tyler.

The example that I left don't show the error. If you test with this other example.

user1:12345678:3.5:2.5:8:1:2:3
user2:12345679:4.5:3.5:8:1:3:2
user3:12345687:5.5:2.6:5.1:1:3:2
user4:12345670:5.5:2.5:5.2:3:2:1
user5:12345671:2.5:5.5:7:2:3:1

You can test that the code doesn't work with the decimals.

About the other questions, I'm using GNU sort and my kernel version is 2.6.12-9-386.

Please post the output you get, given the above input data.

Example

user1:12345678:3.5:2.5:8:1:2:3
user2:12345679:4.5:3.5:8:1:3:2
user3:12345687:5.5:2.5:6:1:3:2
user4:12345670:5.5:2.5:5:3:2:1
user4:12345670:15.5:2.5:5:3:2:1
user4:12345670:15.4:2.5:5:3:2:1
user4:12345670:15.32:2.5:5:3:2:1
user4:12345670:15.62:2.5:5:3:2:1
user5:12345671:2.5:5.5:7:2:3:1

I got ok result.
In some environment decimal delimiter is , or something else
=> change LANG=C before sort, = delimiter is .

Finally it works. It seems like the coma, in Spain we use as float delimiter a coma and not a point, was the problem. If I use the next code it works right:

cat opositores1 | tr "." "," | sort -t ":" -k3,3nr -k4,4nr -k5,5nr

Example of use:

daniel:~$ cat file
user1:12345678:3.5:2.5:8:1:2:3
user2:12345679:4.5:3.5:8:1:3:2
user3:12345687:5.5:2.5:5.1:1:3:2
user4:12345670:5.5:2.5:5.2:3:2:1
user5:12345673:2.5:5.5:7:2:3:1
daniel:~$ cat file | tr "." "," | sort -t ":" -k3,3nr -k4,4nr -k5,5nr
user4:12345670:5,5:2,5:5,2:3:2:1
user3:12345687:5,5:2,5:5,1:1:3:2
user2:12345679:4,5:3,5:8:1:3:2
user1:12345678:3,5:2,5:8:1:2:3
user5:12345673:2,5:5,5:7:2:3:1

Thanks to all for help me.