LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Adding fields and doing grand total (https://www.linuxquestions.org/questions/linux-newbie-8/adding-fields-and-doing-grand-total-625994/)

rainmk 03-05-2008 05:45 PM

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

homey 03-05-2008 07:01 PM

Quote:

The fields are marked in red
Do you want field 4 or do you want 5 which is red?
Are you using bash or perl or whatever?

rainmk 03-05-2008 07:28 PM

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.

homey 03-05-2008 07:45 PM

What do you have so far?

jschiwal 03-05-2008 07:56 PM

Awk is often used for this sort of thing.

chrism01 03-05-2008 07:58 PM

You could use
cut -d',' etc

rainmk 03-06-2008 12:59 PM

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

homey 03-06-2008 07:00 PM

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 10:37 PM.