Summing lines in a file | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Summing lines in a file

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 10-10-2012
LearningLinux2 LearningLinux2 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 15 October 2012, 11:27 AM EDT
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
Summing lines in a file

Can anyone tell me how sum values in each record of a file and append that value to the end? For instance a typical record will be:


Code:
FY12,Budget,771100,,,,,,,,,250,-250

I'd like the record to become

Code:
FY12,Budget,771100,,,,,,,,,250,-250,0

which can be put into another file.
Thank you.

Last edited by Scrutinizer; 10-11-2012 at 02:00 PM.. Reason: code tags
Sponsored Links
    #2  
Old 10-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 7 November 2014, 10:50 AM EST
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts

Code:
awk -F, '{for(i=4;i<=NF;i++){s+=$i}{$(NF+1)=s}}1' OFS="," file


Last edited by pamu; 10-11-2012 at 01:49 AM.. Reason: coorected..
The Following User Says Thank You to pamu For This Useful Post:
LearningLinux2 (10-11-2012)
Sponsored Links
    #3  
Old 10-11-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 23 September 2014, 12:09 PM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts
or:

Code:
awk -F, 'BEGIN{OFS=","}{for(i=4;i<=NF;i++)s+=$i;print $0,s}' infile > another_file


Last edited by rdrtx1; 10-11-2012 at 12:29 AM..
The Following User Says Thank You to rdrtx1 For This Useful Post:
LearningLinux2 (10-11-2012)
    #4  
Old 10-11-2012
LearningLinux2 LearningLinux2 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 15 October 2012, 11:27 AM EDT
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
Thank you both very much for the quick response!

Both code sets worked great but what I neglected to mention is that the file has many records and I need to sum record by record and not carry the total.
Sponsored Links
    #5  
Old 10-11-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 7 November 2014, 10:50 AM EST
Posts: 1,650
Thanks: 58
Thanked 478 Times in 474 Posts
try this



Code:
awk -F, '{for(i=4;i<=NF;i++){s+=$i}{$(NF+1)=s;s=0}}1' OFS="," file

The Following User Says Thank You to pamu For This Useful Post:
LearningLinux2 (10-11-2012)
Sponsored Links
    #6  
Old 10-11-2012
LearningLinux2 LearningLinux2 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 15 October 2012, 11:27 AM EDT
Posts: 3
Thanks: 3
Thanked 0 Times in 0 Posts
Simply amazing! Thank you Pamu!!!
Sponsored Links
    #7  
Old 10-15-2012
AvinashPatil06 AvinashPatil06 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 12 November 2012, 8:02 AM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
awk -F "," ' { for( Cnt=NF-1; Cnt<=NF ; Cnt++) { Val+=$Cnt ; }
print $0","Val ;

} ' In_File_Name >> Out_File_Name
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
HELP with Unix scripts in summing columns in a file. ramneim Homework & Coursework Questions 3 08-21-2012 11:42 AM
Summing over specific lines and replacing the lines with the sum kaaliakahn Shell Programming and Scripting 4 07-05-2012 10:18 PM
Summing over specific lines and replacing the lines with the sum using sed, awk kaaliakahn Shell Programming and Scripting 3 06-25-2012 06:05 PM
Summing file size and output ramkrix UNIX for Advanced & Expert Users 1 11-12-2008 01:30 PM
Summing the columns of a file asahlot Shell Programming and Scripting 3 09-24-2008 10:18 PM



All times are GMT -4. The time now is 10:18 PM.