2

I have a problem I have been trying to figure out:

We have a stock file CSV, which contains the stock in multiple locations.

The csv looks like this:

stock_no,primary,secondary,tertiary,cstock,direct
ABU0029843,1,,,5,
ABU0029934,60,,,5,
ABU0030034,,30,,5,

I would like the end result to look something like this (essentially summing up and removing the empty columns.)

stock_no,primary
ABU0029843,6
ABU0029934,65
ABU0030034,35

I have tried various methods with awk, but I seem to be returning values of 0

However, I am not too familiar with awk, so I am sure I am doing something wrong. Any help would be appreciated.

1

You can try following awk:

awk 'BEGIN { FS = OFS = ","; } NR == 1 { print $1, $2; next; } { for (x = 3; x <= NF; x++) $2 += $x;  print $1, $2 } ' file
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.