LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2008, 06:45 PM   #1
rainmk
LQ Newbie
 
Registered: Mar 2008
Posts: 8

Rep: Reputation: 0
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

Last edited by rainmk; 03-06-2008 at 01:59 PM.
 
Old 03-05-2008, 08:01 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
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?
 
Old 03-05-2008, 08:28 PM   #3
rainmk
LQ Newbie
 
Registered: Mar 2008
Posts: 8

Original Poster
Rep: Reputation: 0
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.
 
Old 03-05-2008, 08:45 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
What do you have so far?
 
Old 03-05-2008, 08:56 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
Awk is often used for this sort of thing.
 
Old 03-05-2008, 08:58 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
You could use
cut -d',' etc
 
Old 03-06-2008, 01:59 PM   #7
rainmk
LQ Newbie
 
Registered: Mar 2008
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 03-06-2008, 08:00 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Maybe, something like this...
Code:
awk -F, '{sum += ((($8+$9) * $5)/1000)}{print$0"\t"sum}' file.txt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find grand total size of all files in a directory fakhrul Red Hat 4 04-17-2010 01:28 AM
Grand Rapids, MI? JesusFreak84 Linux User Groups (LUG) 1 02-02-2010 08:32 AM
Grand entrance prodigy0987 LinuxQuestions.org Member Intro 1 12-16-2006 03:51 PM
New site grand opening! erat123 General 1 11-21-2006 04:38 AM
PHP form-trouble adding extra fields fdlx4000 Programming 6 09-24-2005 10:27 AM


All times are GMT -5. The time now is 05:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration