Skip to content

Instantly share code, notes, and snippets.

@MartGro
Created December 3, 2021 11:42
Show Gist options
  • Save MartGro/d4ea00c899d0b649be93604bad801c94 to your computer and use it in GitHub Desktop.
Save MartGro/d4ea00c899d0b649be93604bad801c94 to your computer and use it in GitHub Desktop.
Sort CSV file based on numerical value of fist field (shell command)
{ head -n1 unsorted.csv && tail -n+2 unsorted.csv | sort -n --field-separator="," -k 1,1; } > sorted.csv;
@MartGro
Copy link
Author

MartGro commented Dec 3, 2021

This is a small shell command that uses the gnu sort command to sort a csv file larger than memory.
It is quite fast.
References:
https://superuser.com/questions/33362/how-to-unix-sort-by-one-column-only
-> how to sort by a single column
https://stackoverflow.com/questions/2188199/how-to-use-double-or-single-brackets-parentheses-curly-braces
-> how to concatenate the output, Attention, the spaces and the semicolons are necessary
https://unix.stackexchange.com/questions/345076/sorting-csv-file-by-first-column-ignoring-header
-> how to sort keeping the header fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment