Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a CSV file from greatest to least based a number appearing in a column

Tags:

bash

sorting

I have a CSV file like this:

bear,1
fish,20
tiger,4

I need to sort it from greatest to least number, based on what is found in the second column, e.g.:

fish,20
tiger,4
bear,1

How can the file be sorted in this way?

like image 664
Village Avatar asked Dec 27 '22 02:12

Village


1 Answers

sort -t, -k+2 -n -r filename

will do what you want.

-t, specifies the field separator to be a comma

-k+2 specifies the field to sort on (field2)

-r specifies a reverse sort

-n specifies a numeric sort

like image 55
Brian Agnew Avatar answered May 19 '23 01:05

Brian Agnew