sort decimal numbers from csv file in bash

Viewed 562

I have this csv file:

A,0,10
B,20,.66
C,2,.72
D,1,.42
E,0,0
F,0,0
G,2,.56

I need to sort that based on the 3rd column so it will look like this:

A,0,10
C,2,.72
B,20,.66
G,2,.56
D,1,.42
E,0,0
F,0,0

I have tried with:

sort -t, -V -k3 input.txt

but its not giving the correct result. Any suggestion?

Based on below answer I have tried:

sort -t, -nr -k3 input.txt

but that gives:

C,2,.72
B,20,.66
G,2,.56
D,1,.42
A,0,10
F,0,0
E,0,0

which is not the expected result as I provided above.

Based on this: https://unix.stackexchange.com/questions/292087/how-do-i-get-bc-to-start-decimal-fractions-with-a-leading-zero

I am now prefixing with 0 and that gives the correct result when sorting.

1 Answers

using numeric sort will do the trick:

sort -t, -nr -k3 1.txt

You are using Version sort which is why you are getting the ouput that you are getting.

Related