Skip to main content
3 of 4
deleted 80 characters in body; edited tags
αғsнιη
  • 41.9k
  • 17
  • 75
  • 118

How to do calculation for each row

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