Parsing and sum column from different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing and sum column from different files
# 1  
Old 03-16-2010
Question Parsing and sum column from different files

Hy everybody,

I have three file with the same formating. I need to parse those files in order that for each first column of the first file, sum the second columns of first,second and third files. Can someone help me ?

example :

# more file1
00:00:12,137;7,0333333
# more file2
00:00:22,672;7,15
# more file3
00:00:20,097;7,016667

the output that i nedd is :

00:00:12;21,2000003

Thanks in advace
# 2  
Old 03-16-2010
The orignal files have 4 columns (split by ; or ,), but the output has only 3 columns. Not sure which colum do you need sum, and which column do you need keep in first column.

Give you a try, you can refer for your own request.

Code:
$ awk -F ";|," '{sum[$1]=sum[$1]+$3} END {for (i in sum) print i,sum[i]}' file*
00:00:20 7
00:00:12 7
00:00:22 7

If you only sum $3, you can run :

Code:
awk -F ";|," '{sum=sum+$3} END {print sum}' file*

# 3  
Old 03-17-2010
Code:
awk -F ";|," '{if(!var){ var=$1;}sum1+=$3;sum2+=$4} END {print var","sum1","sum2}' file*

00:00:12,21,350015

HTH,
PL
# 4  
Old 03-17-2010
Thank you Guys, but I forgot to mention that each file has more than one line and waht I need is for each first column (time) of the first file , sum the second column of the others files

file1
00:00:22,672;7,15
00:01:22,672;9,316667
00:02:22,679;18,5

file 2

00:00:25,672;9,15
00:01:22,672;10,316667
00:02:22,679;18,35

file 3

00:00:29,672;7,15
00:01:22,672;9,316667
00:02:22,679;18,5

output

($1 of the first file);(sum of $2 of the other files for each line)

00:00:22;(7,15+9,15+7,15)
00:01:22;(9,316667+10,316667+9,316667)
00:02:22;(18,5+18,35+18,5)
# 5  
Old 03-18-2010
Question Help !

Hi all,

Any new idea about this issue ?

Tks.
# 6  
Old 03-18-2010
nawk -f rob.awk file1 file2 file3 fileN

rob.awk:
Code:
BEGIN {
  FS=OFS=";"
}
FNR==NR { f1v[FNR]+=$2; f1n[FNR]=$1;next}
{ f1v[FNR]+=$2}
END {
  while(++i in f1v)
    printf("%s%c%.2f\n", f1n[i], OFS, f1v[i])
}

# 7  
Old 03-18-2010
Hi:

Code:
paste -d\; f1 f2 f3 | awk -F\; '{print $1,$2+$4+$6}' OFS=\;

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum of a column as new column based on header in a script

Hello, I am trying to store sum of a column as a new column inside a file but have to find the column names dynamically I/p c1,c2,c3,c4,c5 10,20,30,40,50 20,30,40,50,60 If i want to find sum only column c1, c3 and output it as c6,c7 O/p c1,c2,c3,c4,c5,c6,c7 10,20,30,40,50,30,70... (6 Replies)
Discussion started by: mkathi
6 Replies

2. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

3. UNIX for Beginners Questions & Answers

Sum the values in the column using date column

I have a file which need to be summed up using date column. I/P: 2017/01/01 a 10 2017/01/01 b 20 2017/01/01 c 40 2017/01/01 a 60 2017/01/01 b 50 2017/01/01 c 40 2017/01/01 a 20 2017/01/01 b 30 2017/01/01 c 40 2017/02/01 a 10 2017/02/01 b 20 2017/02/01 c 30 2017/02/01 a 10... (6 Replies)
Discussion started by: Booo
6 Replies

4. UNIX for Dummies Questions & Answers

Match sum of values in each column with the corresponding column value present in trailer record

Hi All, I have a requirement where I need to find sum of values from column D through O present in a CSV file and check whether the sum of each Individual column matches with the value present for that corresponding column present in the trailer record. For example, let's assume for column D... (9 Replies)
Discussion started by: tpk
9 Replies

5. Shell Programming and Scripting

Need to sum up a column value from multiple files into Credit and Debit categories using awk command

i have multiple files with Batch Header, Record detail & Batch trailer data in the files like : BH 20150225950050N8262 RD 20140918000000 99999999 unk Deferred Settlement -13950 BT01 -13950 *Above sample data donot have the spaces coorectly defined. I do have multiple batch trailer... (1 Reply)
Discussion started by: kcdg859
1 Replies

6. Shell Programming and Scripting

Sum values of specific column in multiple files, considering ranges defined in another file

I have a file (let say file B) like this: File B: A1 3 5 A1 7 9 A2 2 5 A3 1 3 The first column defines a filename and the other two define a range in that specific file. In the same directory, I have also three more files (File A1, A2 and A3). Here is 10 sample lines... (3 Replies)
Discussion started by: Bastami
3 Replies

7. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

8. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

9. Shell Programming and Scripting

Sum Of Column Based On Column Condition

I have a following inputfile MT,AP,CDM,TTML,MUM,GS,SUCC,3 MT,AP,CDM,TTSL,AP,GS,FAIL,9 MT,AP,CDM,RCom,MAH,GS,SUCC,3 MT,AP,CDM,RTL,HP,GS,SUCC,1 MT,AP,CDM,Uni,UPE,GS,SUCC,2 MT,AP,CDM,Uni,MUM,GS,SUCC,2 TTSL,AP,GS,MT,MAH,CDM,SUCC,20 TTML,AP,GS,MT,MAH,CDM,FAIL,10... (2 Replies)
Discussion started by: siramitsharma
2 Replies

10. Shell Programming and Scripting

Sum of a column in multiple files

I am performing the following operation on a file that looks like this 1000 0 10 479.0 1115478.07497 0.0 0.0 0.0872665 1000 10 20 1500.0 3470012.29304 0.0 0.0 0.261799 1000 20 30 2442.0 5676346.87758 0.0 0.0 0.436332 1000 30 40 3378.0 7737905.30957 0.0 0.0 0.610865 1000 40 50 4131.0... (2 Replies)
Discussion started by: kayak
2 Replies
Login or Register to Ask a Question