Adding fields and doing grand total
I am trying to do a running as well as grand total total on three fields. The fields are marked in red.
basically this is what i want. ((field8 + field9) * field4) /1000 this value should be added as a 10th field in the same line and at the bottom of the file I can have a grand total of 10th field. 12022997382,32473445360,2008-02-05 04:11:11,11031103,4,12,USA-CANADA,30,0 12023401411,447817660302,2008-02-02 11:58:51,11031103,1,12,USA-CANADA,30,0 12023905389,32495233633,2008-02-28 04:35:03,22882288,5,12,USA-CANADA,30,0 12024157808,40744488734,2008-02-12 08:54:58,11031103,3,12,USA-CANADA,30,0 12024468829,35796531829,2008-02-05 18:34:45,11031103,12,12,USA-CANADA,30,0 |
Quote:
Are you using bash or perl or whatever? |
I am sorry I forgot to mention I am using bash. The fields that are hight lighted I need to perform the above calculations on them.
|
What do you have so far?
|
Awk is often used for this sort of thing.
|
You could use
cut -d',' etc |
This is what I have. This works great for each line total but I have no clue how can I do a grand total.
awk -F , '{printf "%-15s, %-22s, %-5s, %-22s, %-10s, %-6s, %-22s, %-4s, %-4s, %-6f \n", $1 ,$2, $3, $4, $5, $6, $7, $8 , $9, expr (($8+$9)/1000) }' /tmp/detail > /tmp/det |
Maybe, something like this...
Code:
awk -F, '{sum += ((($8+$9) * $5)/1000)}{print$0"\t"sum}' file.txt |
All times are GMT -5. The time now is 11:04 PM. |