Sort CSV file based on first column

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

I have this huge file with over 150k lines, so I can do it in excel :( is there an alternative way?

+15
sorting unix bash shell csv
source share
3 answers

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

-k1 sorted by column 1.

-n sorted numerically instead of lexicographical (so that "11" will not be earlier than "2,3 ...").

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

+33
source share

I do not know why the above solution did not work 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 team solved my problem.

sort -t "," -k1n, 1 fileName

+2
source share

Using csvsort .

  1. Install csvkit if it is not already installed.

     brew install csvkit 
  2. Sort CSV by first column.

     csvsort -c 1 original.csv > sorted.csv 
-one
source share

All Articles