I have a CSV file filename.csv with the following contents.
filename.csv:
"Afghanistan","94.0","81.1"
"Bahamas","42.9","43.2"
"Bolivia (Plurinational State of)","86.7","31.9"
"Brazil","76.7","0.0"
I want to compute the difference between two columns (Column 2 - Column 3), and paste the output to the fourth column. After, I want to numerically sort by the fourth column. However, the command I am using is not sorting by the fourth column.
Command I used: awk -F'","' '{ print $0, $2 - $3 }' filename.csv | sort -k4 -n
Output I got:
"Afghanistan","94.0","81.1" 12.9
"Bahamas","42.9","43.2" -0.3
"Bolivia (Plurinational State of)","86.7","31.9" 54.8
"Brazil","76.7","0.0" 76.7
Expected output file:
"Bahamas","42.9","43.2","-0.3"
"Afghanistan","94.0","81.1","12.9"
"Bolivia (Plurinational State of)","86.7","31.9","54.8"
"Brazil","76.7","0.0","76.7"
Any help is appreciated. Thanks!
foo | sortdoesn’t give you the final result you want, look at the output fromfoowithout runningsort. (foohere represents yourawkcommand.) Obviously you are gettingvalue₁,value₂,value₃ value₄with a space betweenvalue₃andvalue₄instead of a comma. So you don’t even have a fourth column. (Also,value₄is not surrounded by quotes.)sortwill never give you the output you want if you don’t give it the input you intend to give it.