summing from two different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting summing from two different files
# 1  
Old 02-06-2012
summing from two different files

I have two files
Code:
hhhh                                                           3674.00
a                   75 1535  183 2134  291 2452  442 2738  704 3048
a                 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610
a                 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985

another file
Code:
hhhh             644    315927.86   9482704.51                  0                                              
a                   75 1535  183 2134  291 2452  442 2738  704 3048
a                 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610
a                 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985

i want the o/p as
Code:
hhhh             644    315927.86   9482704.51                  3674                                            
a                   75 1535  183 2134  291 2452  442 2738  704 3048
a                 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610
a                 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985

position of the characters should not change
plz help

Last edited by Franklin52; 02-06-2012 at 06:48 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 02-06-2012
Hi Indra2011,

Give a try to this awk script:
Code:
$ cat file1
hhhh 3674.00
a 75 1535 183 2134 291 2452 442 2738 704 3048
a 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610
a 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985
$ cat file2
hhhh 644 315927.86 9482704.51 0
a 75 1535 183 2134 291 2452 442 2738 704 3048
a 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610
a 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985
$ awk 'FNR == NR { value = $2 ; nextfile } FNR == 1 { $NF = $NF + value } { print }' file1 file2                                                                                                                                            
hhhh 644 315927.86 9482704.51 3674                                                                                                                                                                                                           
a 75 1535 183 2134 291 2452 442 2738 704 3048                                                                                                                                                                                                
a 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610                                                                                                                                                                                          
a 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985

Regards,
Birei
# 3  
Old 02-06-2012
Code:
awk 'NR==FNR{if(/^hhhh/)x=$NF;next}/^hhhh/{$NF+=x}1' f1 f2

Code:
$ cat f1
hhhh 3674.00
a 75 1535 183 2134 291 2452 442 2738 704 3048
a 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610
a 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985

Code:
$ cat f2
hhhh 644 315927.86 9482704.51 0
a 75 1535 183 2134 291 2452 442 2738 704 3048
a 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610
a 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985

Code:
$ awk 'NR==FNR{if(/^hhhh/)x=$NF;next}/^hhhh/{$NF+=x}1' f1 f2
hhhh 644 315927.86 9482704.51 3674
a 75 1535 183 2134 291 2452 442 2738 704 3048
a 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610
a 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985

# 4  
Old 02-06-2012
Hi Birei,

1st file,
Code:
SPNT                  644    213906.40   9402442.90             0
VELF                 4516 1504 4620 1510 4758 1521 4900 1534 5267 1566
VELF                 5594 1626 5890 1778 6165 1925 6566 2215 7004 2659
SPNT                  724    214169.15   9402868.20             0
VELF                 4510 1503 4605 1505 4747 1518 4871 1526 5122 1552
VELF                 5293 1571 5481 1591 5736 1667 5828 1735 6107 1877
VELF                 6362 2022 7004 2579
etc
etc

next file
Code:
SPNT                                                           1002.00
VELF                 4516 1504 4620 1510 4758 1521 4900 1534 5267 1566
VELF                 5594 1626 5890 1778 6165 1925 6566 2215 7004 2659
SPNT                                                           1029.00
VELF                 4510 1503 4605 1505 4747 1518 4871 1526 5122 1552
VELF                 5293 1571 5481 1591 5736 1667 5828 1735 6107 1877
VELF                 6362 2022 7004 2579

output should be like
Code:
SPNT                  644    213906.40   9402442.90             1002
VELF                 4516 1504 4620 1510 4758 1521 4900 1534 5267 1566
VELF                 5594 1626 5890 1778 6165 1925 6566 2215 7004 2659
SPNT                  724    214169.15   9402868.20             1029
VELF                 4510 1503 4605 1505 4747 1518 4871 1526 5122 1552
VELF                 5293 1571 5481 1591 5736 1667 5828 1735 6107 1877
VELF                 6362 2022 7004 2579
etc
etc


plz help!!

Last edited by Franklin52; 02-06-2012 at 09:10 AM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 02-06-2012
Make sur your file have the same number of SPNT

Code:
awk 'NR==FNR{if(/SPNT/)a[++c]=$NF;next}/^SPNT/{$NF+=a[++i]}1' yournextfile yourfirstfile

---------- Post updated at 12:08 PM ---------- Previous update was at 12:01 PM ----------

Code:
$ cat f1
SPNT 644 213906.40 9402442.90 0
VELF 4516 1504 4620 1510 4758 1521 4900 1534 5267 1566
VELF 5594 1626 5890 1778 6165 1925 6566 2215 7004 2659
SPNT 724 214169.15 9402868.20 0
VELF 4510 1503 4605 1505 4747 1518 4871 1526 5122 1552
VELF 5293 1571 5481 1591 5736 1667 5828 1735 6107 1877
VELF 6362 2022 7004 2579

Code:
$ cat f2
SPNT 1002.00
VELF 4516 1504 4620 1510 4758 1521 4900 1534 5267 1566
VELF 5594 1626 5890 1778 6165 1925 6566 2215 7004 2659
SPNT 1029.00
VELF 4510 1503 4605 1505 4747 1518 4871 1526 5122 1552
VELF 5293 1571 5481 1591 5736 1667 5828 1735 6107 1877
VELF 6362 2022 7004 2579

Code:
$ awk 'NR==FNR{if(/SPNT/)a[++c]=$NF;next}/^SPNT/{$NF+=a[++i]}1' f2 f1
SPNT 644 213906.40 9402442.90 1002
VELF 4516 1504 4620 1510 4758 1521 4900 1534 5267 1566
VELF 5594 1626 5890 1778 6165 1925 6566 2215 7004 2659
SPNT 724 214169.15 9402868.20 1029
VELF 4510 1503 4605 1505 4747 1518 4871 1526 5122 1552
VELF 5293 1571 5481 1591 5736 1667 5828 1735 6107 1877
VELF 6362 2022 7004 2579

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Summing all fields in a file

I was playing around to see how stuff works, and was trying to sum all fields in a file. cat file 1 2 3 4 5 6 7 8 9 10 11 12 I made this script: awk 'BEGIN {OFS=RS}{$1=$1}{s+=$0} END {print "sum="s}' file This gives 15, why not 78? I test it like this awk 'BEGIN... (5 Replies)
Discussion started by: Jotne
5 Replies

2. Shell Programming and Scripting

Summing columns in line

I have a file with the following format AAAAA 1.34B 0.76B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.00B 0.90B 0.00B 0.00B 0.46B 0.00B 0.03B 0.00B ... (4 Replies)
Discussion started by: ncwxpanther
4 Replies

3. UNIX for Dummies Questions & Answers

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: FY12,Budget,771100,,,,,,,,,250,-250 I'd like the record to become FY12,Budget,771100,,,,,,,,,250,-250,0 which can be put into another file. Thank you. (6 Replies)
Discussion started by: LearningLinux2
6 Replies

4. Shell Programming and Scripting

Reading several files and summing their content line-by-line

Hey, I am gettin a bit crazy with my script. I have several input datas with the same name (5.ill) in different folders (daysim_01, daysim_02, etc.). The 4. column of each of the data has to be summed with each other and then hass to be written in one new file. So file1: 1 1 0 1 2 1 1 2 ... (7 Replies)
Discussion started by: ergy1983
7 Replies

5. Shell Programming and Scripting

Summing column value - using PERL

I'm new to perl programming. I've a csv file as below. 20100221, abc_1, 200 20100221, abc_4, 350 20100221, opq_3, 200 20100221, abc_5, 220 20100221, xyz_1, 500 20100221, abc_2, 500 20100221, abc_3, 100 20100221, xyz_2, 700 20100221, opq_2, 350 20100221, xyz_3, 100 20100221, opq_1, 230... (8 Replies)
Discussion started by: ganapati
8 Replies

6. Shell Programming and Scripting

Summing amounts

Hi I need to sum of sub total amounts. Please any ideas file1.txt type: xx xyz 200 300 xyz1 300 400 Sub total 500 700 type:yy abc 200 300 abc1 100 100 Sub total ... (4 Replies)
Discussion started by: mohan705
4 Replies

7. Shell Programming and Scripting

summing numbers in files

I am trying to take two files and add the numbers from each. There is a total of 5192 numbers in each file and I want to add them row by row... ie. first row of file 1 + first row of file 2 = first row of output. Eventually I will be summing 40401 of these files together but starting with 2 just... (21 Replies)
Discussion started by: pattywac
21 Replies

8. Shell Programming and Scripting

A summing issue

Hi All, Here is the problem: The input file is like as per below. Each record has 30 chars in total. Have to add the first 17 and the next 13 and append the output. Total records can vary. 000000004728800000000000003908 000000003005100000000000002484 000000002602200000000000002151... (5 Replies)
Discussion started by: er_ashu
5 Replies

9. UNIX for Dummies Questions & Answers

summing according to the column

I have a text file with two columns the first column is an integer and the second column is date how do i sum up the first column according to the date example 123 jan1 232 jan1 473 jan2 467 jan2 356 jan3 376 jan3 my result should be 355 jan1 940 jan2 732 jan3 how do i... (2 Replies)
Discussion started by: ramky79
2 Replies

10. Shell Programming and Scripting

Summing on column

Hi Friends How to do sum on a column? I have a file like: FRED 500.01 TX SMITH 50.10 NY HARRY 5.00 CA 555.11 Sum on second column. I am trying using nawk like nawk 'BEGIN {FS="|"}; {printf $1"+"}' Thanks a lot for your help S :) (2 Replies)
Discussion started by: sbasetty
2 Replies
Login or Register to Ask a Question