3

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.

6
  • That is strange I got the exact output that you suggested Commented Aug 27, 2018 at 8:44
  • 2
    Your locale might've messed the sorting, trying using LC_ALL=C sort .... Commented Aug 27, 2018 at 8:46
  • u123, Have you checked that your original file is unchanged from the original to be sorted state with your trial of the suggested answer, numeric sort. Should not make any difference, but just check answer with a correct original file afresh. Otherwise, try PesaThe in their comment above. If that works, let us know. Interested. Commented Aug 27, 2018 at 9:24
  • Where do I put: LC_ALL=C sort ... ? Commented Aug 27, 2018 at 9:38
  • @u123 just before executing sort in the terminal before sort use LC_ALL=C Commented Aug 27, 2018 at 9:39

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

In OSX, copy and paste original text for sort to Text Edit. Convert to plain text. Change .txt to .csv. Then in Bash run this answer, I get the correct desired result. That which OP seeks.
@Cam_Aust Thank you for the information, I got the result OP wants with .txt as well , are you getting a different result when you are doing it with .txt ???
Confirm yes. Same result when a .txt file.
Running: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15) OSX 10.11.6.
@Cam_Aust Most like I think OP is facing the issue that PesaThe suggested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.