简体   繁体   中英

sort decimal numbers from csv file in bash

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM