Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort CSV file based on first column

Is there a way to sort a csv file based on the 1st column using some shell command?

I have this huge file with more than 150k lines hence I can do it in excel:( is there an alternate way ?

like image 223
fiddle Avatar asked Oct 08 '14 04:10

fiddle


People also ask

How do I sort a CSV file in ascending order in Python?

To sort CSV by multiple columns, use the sort_values() method. Sorting by multiple columns means if one of the columns has repeated values, then the sort order depends on the 2nd column mentioned under sort_values() method.

How do I sort a CSV file in one column in Python?

To sort CSV by a single column, use the sort_values() method. Set the column using which you want to sort in the sort_values() method.


2 Answers

I don't know why above solution was not working in my case.

15,5 17,2 18,6 19,4 8,25 8,90 9,47 9,49 10,67 10,90 13,96 159,9 

however this command solved my problem.

sort -t"," -k1n,1 fileName 
like image 41
Bharthan Avatar answered Sep 29 '22 18:09

Bharthan


sort -k1 -n -t, filename should do the trick.

-k1 sorts by column 1.

-n sorts numerically instead of lexicographically (so "11" will not come before "2,3...").

-t, sets the delimiter (what separates values in your file) to , since your file is comma-separated.

like image 75
Travis Avatar answered Sep 29 '22 20:09

Travis