I have a script which creates/modifies data into a formatted csv.
The trailer record should display 2 columns, the first is a static entry of "T" to identify it as a trailer record. The 2nd is a total of amounts in a column throughout the entire file.
My total isn't displaying correctly. I do not use decimal points and the following code is not displaying the final digit. It displays every other digit correctly (just missing the 2nd digit after the decimal point)
At the minute I have
$(awk '{s+=$3}END {print "T, "s}
On a seperate note, considering it is the 3rd column I am totalling, the header record has 3 columns which I do not want included - it seems to work just now ever though the header has a fixed number in the 3rd column (does awk count the $'s from 0 or from 1?)
A, blah, 091929
C, 123491,4999,13122010,C, 8281946 MR BRIAN BOBS ,ND
T, 499
Expect output =
A, blah, 091929
C, 123491,4999,13122010,C, 8281946 MR BRIAN BOBS ,ND
T, 4999
As you can see the trailer record should hold both decimal places, but it doesn't. Considering the file is a csv, the field length is variable - the only thing I can confirm is the last 2 digits in the column will be the 2 digits on the right of the decimal place.
I add the header record myself near the end of the script, by the time I have done this I have already created the trailer record - meaning the total is complete and doesn't use the header.
Anymore thoughts as to why my code is not correctly using 2 decimal places?
Hmmm...... I don't quite understand your question and/or reference to the '2 decimal places'.
Can you maybe rephrase it as I don't see any 'decimal places' - you're dealing with integers.
Does the alternate solution solve your issue?
sorry, the 3rd column in my sample is basically an amount field. I have removed the decimal places for other reasons. Anyway, as it is an amount field the the final 2 digits are the 2 on the right of the decimal place.
Even so, my problem is the code I am using is not correctly displaying the total, it is missing the right hand digit.
$(awk '{s+=$3}END
Hmmm..... so what you're saying is that you used to have floats values in the 3-rd field. Then you converted all 3-rd field values to integers (removed the fractions). And now you're expecting the float values for the trailer/sum row?
How's that possible?
I must be uber confused. Maybe other could understand the dilemma better....
I am going to try and be a bit clearer. I use the following code to display the total of all fields in a column - $(awk '{s+=$3}END {print "T, "s}'. This prints "T, Total"