I had a csv file of data such like that when read into shell:
name,income,reward,payment
Jackson,10000,2000,1000
Paul,2500,700,200
Louis,5000,100,1800
and I want to find the net earning for each person, use formula: "net = income+reward-payment".
when I used command to do this, it only calculate the first row of data.
$ cat data.csv | awk -F ',' '{for (i=1;i<=NF;i++) net[i] = $2+$3-$4} END {for (p in total) print p, "net = ", net[p]}' > result.txt
How can I do the calculation here?
By the way, the names are not unique, so I try (for loop) to create index for the array [net].
My expected output is:
1 Jackson net = 11000
2 Paul net = 3000
3 Louis net = 3300
csvkit
, or a CSV-parsing library for perl (e.g.Text::CSV
) or python (e.g.csv
) or whatever language you're using. This answer is only good for simple comma-delimited input like in the OP's example, not full CSV. – cas May 03 '21 at 07:32