Help with sum of data set


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with sum of data set
# 1  
Old 10-29-2012
Help with sum of data set

Input file
Code:
2         1159,310,           
4         142,199,218,91,     
3         91,273,349,

Desired output result
Code:
2        1469
4        650
3        713

I have long list of input file as shown above.
It has a "," delimited to separate between each record in column 2.
I would like to sum all the record in same line at column 2 to get the desired output result.
The theory behind to manual calculate to get the Desired output result
Code:
2        1159+310=1469
4        142+199+218+91=650
3        91+273+349=713

Thanks for any advice.
# 2  
Old 10-29-2012
Code:
perl -lane '$s=0;$s+=$_ for split /,/,$F[1];print "$F[0] $s"' file

With awk (with some assumptions):
Code:
awk -F'[ \t]+|,' '{s=0;for(i=2;i<NF;i++) s+=$i;print $1,s}' file


Last edited by elixir_sinari; 10-29-2012 at 12:47 AM..
This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk and sum with original data

I am using the cat /home/billing/1|awk '{ SUM += $8} END { print SUM }' >> /home/billing/test I am getting the only total of the all i need to print with all details Source File All-dum-sg--Oc-14-2018 261 dummy server1_root_Silver dummy 0134B TDEV 151451... (4 Replies)
Discussion started by: ranjancom2000
4 Replies

2. Shell Programming and Scripting

Help with sum range of data set together

Input File: 2000 3 1998 2 1997 2 1994 1 1991 1 1989 1 1987 2 1986 2 1985 1 1984 1 . . 10 277256 9 278274 8 282507 7 284837 6 287066 5 292967 (4 Replies)
Discussion started by: perl_beginner
4 Replies

3. Post Here to Contact Site Administrators and Moderators

How to sum up data in fixed width file with decimal point?

HI Everyone, I have below source file AAA|NAME1|ADDRESS1|300.20 BBB|NAME2|ADDRESS2|400.31 CCC|NAME3|ADDRESS3|300.34 I have requirement where I need to sum up fourth field in above fixed width pipe delimited flat file. When I use below code, it gives me value 1001.00 But I am expecting... (1 Reply)
Discussion started by: patricjemmy6
1 Replies

4. Shell Programming and Scripting

Sum from successive lines following date header to create data for graphing connections

Hi, I have a log file created from a load balancer showing connections to each member of a two member pool with the following format (where first field is source IP, second field is load balanced IP address and third field is destination member. I need to plot a graph by date/time and number of... (5 Replies)
Discussion started by: shog63
5 Replies

5. UNIX for Dummies Questions & Answers

How to get the set of files size as a sum in a directory.

Hi, Can someone help me to get the complete files size (sum) over a perod time (1 day,2days)in a directory. eg: Directory :ABC I have a1,a2,a3 files are created in last 24 hours so I need to get the some of all these files. I am using the below find command but its giving me the... (1 Reply)
Discussion started by: gaddamja
1 Replies

6. Shell Programming and Scripting

Help with calculate total sum of same data problem

Long list of input file: AGDRE1 0.1005449050 AGDRE1 2.1005443435 AGDRE1 1.2005449050 AGDRE1 5.1005487870 AASFV3 50.456304789 AASFV3 2.3659706549 AASFV3 6.3489807860 AASFV3 3.0089890148 RTRTRS 5.6546403546 . . Desired output file: AGDRE1 8.5021829410 AASFV3 62.180245240... (2 Replies)
Discussion started by: perl_beginner
2 Replies

7. UNIX for Dummies Questions & Answers

Sum of data in row format

Hi All, I have some numbers in two different files file1 4.21927E+00 4.68257E+00 5.56871E+00 3.59490E+01 7.65806E+01 1.39827E+02 and file2 5.61142E+00 6.21648E+00 7.40152E+00 4.41917E+01 8.31586E+01 1.42938E+02 I would like to get file3 which contains in each column the sum of the... (6 Replies)
Discussion started by: f_o_555
6 Replies

8. UNIX for Advanced & Expert Users

A variable and sum of its value in a huge data.

Hi Experts, I got a question.. In the following output of `ps -elf | grep DataFlow` I get:- 242001 A mqsiadm 2076676 1691742 0 60 20 26ad4f400 130164 * May 09 - 3:02 DataFlowEngine EAIDVBR1_BROKER 5e453de8-2001-0000-0080-fd142b9ce8cb VIPS_INQ1 0 242001 A mqsiadm... (5 Replies)
Discussion started by: varungupta
5 Replies

9. Shell Programming and Scripting

Read the data from multiple files and sum the value

Hi all, I have a requirement where i have to read multiple files using Shell Script in Korn Shell. each file will have the 3rd line as the amount field, i have to read this amount field and sum it for all the files. any idea on how to achieve this?? (i think i can achieve it using a loop,... (9 Replies)
Discussion started by: nvuradi
9 Replies
Login or Register to Ask a Question