|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 01:00 PM.. Reason: code tags |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Code:
awk -F, '{for(i=4;i<=NF;i++){s+=$i}{$(NF+1)=s}}1' OFS="," fileLast edited by pamu; 10-11-2012 at 12:49 AM.. Reason: coorected.. |
| The Following User Says Thank You to pamu For This Useful Post: | ||
LearningLinux2 (10-11-2012) | ||
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
or: Code:
awk -F, 'BEGIN{OFS=","}{for(i=4;i<=NF;i++)s+=$i;print $0,s}' infile > another_fileLast edited by rdrtx1; 10-10-2012 at 11:29 PM.. |
| The Following User Says Thank You to rdrtx1 For This Useful Post: | ||
LearningLinux2 (10-11-2012) | ||
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Simply amazing! Thank you Pamu!!!
|
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
awk -F "," ' { for( Cnt=NF-1; Cnt<=NF ; Cnt++) { Val+=$Cnt ; }
print $0","Val ; } ' In_File_Name >> Out_File_Name |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 10:42 AM |
| Summing over specific lines and replacing the lines with the sum | kaaliakahn | Shell Programming and Scripting | 4 | 07-05-2012 09: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 05:05 PM |
| Summing file size and output | ramkrix | UNIX for Advanced & Expert Users | 1 | 11-12-2008 12:30 PM |
| Summing the columns of a file | asahlot | Shell Programming and Scripting | 3 | 09-24-2008 09:18 PM |
|
|