Sort CSV file based on first column Sort CSV file based on first column unix unix

Sort CSV file based on first column


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.


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

15,517,218,619,48,258,909,479,4910,6710,9013,96159,9

however this command solved my problem.

sort -t"," -k1n,1 fileName


Using csvsort.

  1. Install csvkit if not already installed.

    brew install csvkit
  2. Sort CSV by first column.

    csvsort -c 1 original.csv > sorted.csv