Adding columns in csv

I have the following data in FILE1.CSV:

code: Amount1: Amount2:
xxxxx ,, 200 ,,400
yyxxa ,,200
bbcgu ,,2500 ,,300

i want to be able to produce the following FILE2.CSV:

code: Amount
xxxxx ,, 600
yyxxa ,,200
bbcgu ,,2800

Just want to now how to add the columns in unix and if there is no amount2 then do nothing

thanks for you assistance

awk -F",," '
   $0 !~ /Amount/ {print $1,",,"($2+$3)}
   /Amount/ {print "code: Amount"}
' infile

or written on one line:

awk -F",," '$0 !~ /Amount/ {print $1,",,"($2+$3)}; /Amount/ {print "code: Amount"}' infile

OK that does the trick
could you just help explain the code for me as to what each section is doing so that i may learn from this process rather thangettingthe answer.

Also one problem i am finding is that if the original file had a figure of
,,00003864096.93 it is being displayed as
,,3.8641e+06.

It is imperative that the ammount remans the same and no rounding is done, could you assist.

I didn't check what it does with such long values.

For the code:

Setting awk's input field separator to something useful. Those double commas suited well:

-F",,"
   $0 !~ /Amount/ {print $1,",,"($2+$3)}

$0 -> the whole line
!~ -> is not like
/Amount/ -> is just a pattern, a string in that $0
The curly brackets start the action when "the line has nothing like "Amount" in it
print $1 -> prints the 1st field, which is defined by our separator we have set in the beginning
The comma separating $1 and ",," is putting in the OFS, output field separator which is a blank per default
",," -> simply printing two commas
($2+$3) -> adding the values of field 2 and field 3 (don't forget, the ",," counts as field separator and is left out
Thats for that action so far, so we check the current line with the next pattern/action pair

/Amount/ {print "code: Amount"}

/Amount/ {print "code: Amount"} -> if pattern "Amount" is somewhere in the line, just print out the string "code: Amount"

' infile -> hands over awk the file parse, so no "cat infile| awk ..." is needed, which is useless use of cat.

Thanks again, is there anyomne outhere who can assist with the rounding issue?

Just checked, you can write it like following:

....int($2+$3)....

or any more suitable type.

Or check this:

The GNU Awk User's Guide

thanks you are a star

No, just waiting to get an update through so I can have my weekend :smiley: :smiley: