14

The file looks like

5.1,3.5,1.4,0.2,Banana
4.9,3.0,1.4,0.6,Apple
4.8,2.8,1.3,1.2,Apple

and I need to have it be

4.9,3.0,1.4,0.2,Apple
4.8,2.8,1.3,1.2,Apple
5.1,3.5,1.4,0.2,Banana

I have been trying to use

sort -t, -k5 file.csv > sorted.csv

All it does is make it

5.1,3.5,1.4,0.2,Banana
4.8,2.8,1.3,1.2,Apple
4.9,3.0,1.4,0.6,Apple

How do I make it like this? It does not seem to be sorting it at all.

7
  • 7
    Can't reproduce. Your command works fine over here Commented Sep 5, 2013 at 17:14
  • This question appears to be off-topic because it is about bash and belongs on Unix & Linux. Commented Sep 5, 2013 at 17:14
  • Same here. Sorts just fine for me. Commented Sep 5, 2013 at 17:14
  • 11
    @ColeJohnson Bash is on topic on SO. Just have a look at the Bash tag Commented Sep 5, 2013 at 17:15
  • 1
    @mrm9084 Cannot reproduce either. Have you tried to delete all columns and perform sorting only for Banana and Apple? Did it work? Commented Sep 6, 2013 at 13:04

2 Answers 2

7

GNU sort is locale sensitive, which can cause weirdness. Try the following and see if it makes a difference:

LC_ALL=C sort -t, -k5 file.csv > sorted.csv
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome to Stack Overflow. Please read the About page soon. You're correct that occasionally (or even always if your locale is set to something like en_US.UTF-8, as mine is), the locale can certainly affect the sort order of some data. However, it is hard to see how the locale could affect this — indeed, it is hard to duplicate the problem, let alone sort it out (pun half-intended).
7

Is this what you need to have it be

# sort -t . -nrk2 sorted.csv 
4.9,3.0,1.4,0.6,Apple
4.8,2.8,1.3,1.2,Apple
5.1,3.5,1.4,0.2,Banana

Comments

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.