Sort CSV file from maximum to least based number appearing in a column

I have a CSV file, for example:

bear,1
fish,20
tiger,4

I need to sort it from maximum to least based on what is found in the second column, for example:

fish,20
tiger,4
bear,1

How to sort a file this way?

+3
source share
1 answer
sort -t, -k+2 -n -r filename

will do what you want.

-t, indicates the field separator as a comma

-k+2 indicates a field to sort by (field2)

-r indicates reverse sorting

-n indicates numerical sort

+7
source

All Articles