Hello. I wrote some code for an awk command but I want to learn to turn it into an awk script but am stuck. I have a file (data.csv) that has the following data:
ADD,1,3,5,8,10,11,54
SUB,1,2,3,4
ADD,15,18,21,42,37
ADD,1,1,1,0,0,3,16
ADD,4,1,8,0,4,6,13,16,17,20,8,6,4
SUB,13,8
If the line has ADD, I added. Subtract for lines that start with SUB. I am then to return the sum of all the lines. This is the awk code that I have:
awk -F',' '{if($1 == "ADD") {for(i=2;i<=NF;i++) t+=$i; print t;} else { t=$2;for(i=3;i<=NF;i++) t-=$i;print t; }t=0}' data.csv
The only issue with the output is that it provides a sum for each line but not the total sum. So how would I turn this into a script?