Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting csv file by 5th column using bash

Tags:

linux

bash

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.

like image 235
mrm9084 Avatar asked Sep 05 '13 17:09

mrm9084


People also ask

How do I sort a column in a CSV file in Linux?

You need to use two options for the sort command: --field-separator (or -t ) --key=<start,end> (or -k ), to specify the sort key, i.e. which range of columns (start through end index) to sort by. Since you want to sort on 3 columns, you'll need to specify -k 3 times, for columns 2,2 , 1,1 , and 3,3 .

How do I sort a column in bash?

Sort allows us to sort a file by columns by using the -k option.

How do I sort the third column in Linux?

SF. No, by default sort columns are blank separated, they are not character columns, to sort on the 3rd character column, the syntax would be: sort -k 1.3,1.3 .


1 Answers

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
like image 189
BigSmoke Avatar answered Sep 28 '22 00:09

BigSmoke