I am running the following command to process some CSV data
grep -i "area harvested.*2005" ps1_apricot_countries_2005.csv | sed 's/\"//g'
This results in the following output (top 7 records shown only as sample):
Afghanistan,31,Area Harvested,2005,Ha,5200.00000,F
Africa +,31,Area Harvested,2005,Ha,59536.00000,A
Albania,31,Area Harvested,2005,Ha,400.00000,F
Algeria,31,Area Harvested,2005,Ha,22888.00000,
Americas +,31,Area Harvested,2005,Ha,11496.00000,A
Argentina,31,Area Harvested,2005,Ha,2200.00000,F
Armenia,31,Area Harvested,2005,Ha,5300.00000,
Asia +,31,Area Harvested,2005,Ha,272644.00000,A
As it can be seen this is sorted alphabetically on the first column.
I am trying to pipe this into sort
so that I can sort the above data in descending order based on the 6th numeric comma separated column.
I tried:
grep -i "area harvested.*2005" ps1_apricot_countries_2005.csv | sed 's/\"//g' | sort -k6rn
However this resulted in the following (top 7 records shown only as sample):
Afghanistan,31,Area Harvested,2005,Ha,5200.00000,F
Africa +,31,Area Harvested,2005,Ha,59536.00000,A
Albania,31,Area Harvested,2005,Ha,400.00000,F
Algeria,31,Area Harvested,2005,Ha,22888.00000,
Americas +,31,Area Harvested,2005,Ha,11496.00000,A
Argentina,31,Area Harvested,2005,Ha,2200.00000,F
Armenia,31,Area Harvested,2005,Ha,5300.00000,
It still appears to be sorted on the first column and not the 6th column in descending order. Could anyone please explain how to correct the approach above to achieve this?
You can use this sort:
sort -t, -rnk6
to sort on 6th numeric field descending, delimited by ,
.
-t,
is used to tell sort
that fields are delimited by comma.-rnk6
is used to sort in reverse numerical order on field 6This will give this output:
Asia +,31,Area Harvested,2005,Ha,272644.00000,A
Africa +,31,Area Harvested,2005,Ha,59536.00000,A
Algeria,31,Area Harvested,2005,Ha,22888.00000,
Americas +,31,Area Harvested,2005,Ha,11496.00000,A
Armenia,31,Area Harvested,2005,Ha,5300.00000,
Afghanistan,31,Area Harvested,2005,Ha,5200.00000,F
Argentina,31,Area Harvested,2005,Ha,2200.00000,F
Albania,31,Area Harvested,2005,Ha,400.00000,F
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With