How to sort the columns of a CSV file by the ratio of two columns?

I have a CSV file, for example:

bear,1,2
fish,3,4
cats,1,5
mice,3,3

I want to sort it, from highest to lowest, by the ratio of columns 2 and 3. For example:

bear,1,2 # 1/2 = 0.5
fish,3,4 # 3/4 = 0.75
cats,1,5 # 1/5 = 0.2
mice,3,3 # 3/3 = 1

It will be sorted as follows:

mice,3,3
fish,3,4
bear,1,2
cats,1,5
  • How can I sort the columns from highest to lowest with respect to the two numbers in columns 2 and 3?
+3
source share
3 answers
awk 'BEGIN { FS = OFS = ","} {$4 = $2/$3; print}' | sort -k4,4nr -t, | sed 's/,[^,]*$//' inputfile

or using GNU AWK ( gawk):

awk -F, '{a[$3/$2] = $3/$2; b[$3/$2] = $0} END {c = asort(a); for (i = 1; i <= c; i++) print b[a[i]]}' inputfile

The above methods are better than the following, but it is more efficient than another answer that uses Bash and various utilities:

while IFS=, read animal dividend divisor
do
    quotient=$(echo "scale=4; $dividend/$divisor" | bc)
    echo "$animal,$dividend,$divisor,$quotient"
done < inputfile | sort -k4,4nr -t, | sed 's/,[^,]*$//'

As single line:

while IFS=, read animal dividend divisor; do quotient=$(echo "scale=4; $dividend/$divisor" | bc); echo "$animal,$dividend,$divisor,$quotient"; done < inputfile | sort -k4,4nr -t | sed 's/,[^,]*$//'
+6
source

Why not just create another column that contains the ratio of the second and third columns, and then sort by that column?

+1

bash - .

If you insist ... here is an example:

a=( `cut -d "," -f 2 mat.csv` ); b=( `cut -d "," -f 3 mat.csv` );for i in {0..3};do (echo -n `head -n $((i+1)) mat.csv|tail -1`" "; echo "scale=4;${a[i]}/${b[i]}"|bc) ;done|sort -k 2 -r

Change file name and length.

+1
source

All Articles