Sum of data in row format


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sum of data in row format
# 1  
Old 01-06-2010
Sum of data in row format

Hi All,
I have some numbers in two different files
file1
Code:
4.21927E+00	4.68257E+00	5.56871E+00	3.59490E+01	7.65806E+01	1.39827E+02

and
file2
Code:
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 corresponding columns in file1 and file2

Regards,
Sarah
# 2  
Old 01-06-2010
If your shell supports process substitution:

Code:
paste -d+ <(
  tr '\t' '\n' <file1
  ) <(
        tr '\t' '\n'<file2
        ) | 
          bc | 
            paste >file3 -s


If the input files contain more than one line:

Code:
awk >file3 'getline f2 < "file2" {
  split(f2, t); for (i=0; ++i<=NF;)
    printf "%E%s", $i + t[i], (i == NF ? RS : FS)
	}' file1


Last edited by radoulov; 01-06-2010 at 01:48 PM..
# 3  
Old 01-06-2010
Code:
perl -lane 'for (0..$#F) { $ARGV eq 'file1' ? $_{$_} = $F[$_] : print $_{$_} + $F[$_]}' file1 file2

Smilie
# 4  
Old 01-06-2010
Code:
awk '{for (i=1;i<=NF;i++) a[i]+=$i} END {for (j=1;j<=length(a);j++) printf "%E ", a[j]}' file1 file2

# 5  
Old 01-06-2010
A more systematic and brute force approach ( am I hearing groans...!!! ??? )
Code:
 perl -e 'open(FILE1,"<file1");open(FILE2,"<file2");while(($line1=<FILE1>)&&($line2=<FILE2>)){@num1=split(/\s+/,$line1);@num2=split(/\s+/,$line2);foreach (0..$#num1){print $num1[$_]+$num2[$_];print "\n";}}close(FILE1);close(FILE2);'

Regards
Gaurav
# 6  
Old 01-07-2010
Thank you, problem solved!
# 7  
Old 01-07-2010
And another one that handles more than one line per input file:

Code:
perl -lane'
  @ARGV?push@f,[@F]:(print join"\t",map{sprintf"%E",$f[$c]->[$_]+$F[$_]}0..$#F and$c++)
  ' file1 file2

 
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 numbers in row

Need help in coding: File with several rows incl. numbers like 1 2 3 4 5 6 7 8 ... How can i build the sum of each row seperately? 10 26 ... Thx for help. Please use CODE tags as required by forum rules! (13 Replies)
Discussion started by: smitty11
13 Replies

2. Shell Programming and Scripting

Sum specified values (columns) per row

Hello out there, file.txt: comp51820_c1_seq1 42 N 0:0:0:0:0:0 1:0:0:0:0:0 0:0:0:0:0:0 3:0:0:0:0:0 0:0:0:0:0:0 comp51820_c1_seq1 43 N 0:0:0:0:0:0 0:1:0:0:0:0 0:0:0:0:0:0 0:3:0:0:0:0 0:0:0:0:0:0 comp51820_c1_seq1 44 N 0:0:4:0:3:1 0:0:1:9:0:0 10:0:0:0:0:0 0:3:3:2:2:6 2:2:2:5:60:3... (16 Replies)
Discussion started by: pathunkathunk
16 Replies

3. Shell Programming and Scripting

Sum value in a row and print the max

I have the input file in attached. I want the output file : Date , Time , Max_Bearer 11/01/2013 , 23:00 , 1447.894167 11/02/2013 , 00:00 , 1429.266667 11/03/2013 , 00:00 , 712.3175 11/04/2013 , 22:00 , 650.9533333 11/05/2013 , 23:00 , 665.9558333 11/06/2013 , 23:00 , 659.8616667... (2 Replies)
Discussion started by: justbow
2 Replies

4. Emergency UNIX and Linux Support

[Solved] Mysql - Take data from row and copy it to another row

Sorry if I repost my question in this section, but I'm really in a hurry since I have to finish my work... :( Dear community, I have a table with two rows like: Row1 Row2 ======= ======= 7,3 text 1 1,3 text 2 1,2,3 blabla What i need to do is add/copy... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

5. Shell Programming and Scripting

awk - script help: column to row format of data allignment?

Experts Good day, I have the following data, file1 BRAAGRP1 A2X B2X C2X D2X BRBGRP12 A3X B3X Z10 D09 BRC1GRP2 LO01 (4 Replies)
Discussion started by: rveri
4 Replies

6. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

7. Shell Programming and Scripting

Format row data into columns

I have a file which looks like this: /* ----------------- EDW$MOC139_R_NNA_BR_SUM_FACT2 ----------------- */ insert_job: EDW$MOC139_R_NNA_BR_SUM_FACT2 job_type: c command: /home/btchproc/load_process/batch_files/batch_nna_brn_split_sum_fact2.sh m machine: edwprod02.dsm.pwj.com #owner:... (29 Replies)
Discussion started by: Gangadhar Reddy
29 Replies

8. Shell Programming and Scripting

Sum of values coming in a row

Hi, my requirement is to sum values in a row. eg: input is: sum,value1,value2,value3,.....,value N Required Output: sum,<summation of N values> Please help me... (5 Replies)
Discussion started by: MrGopal666
5 Replies

9. Shell Programming and Scripting

Format - Inventory Row data into Column - Awk - Nawk

Hi All, I have the following file that has computer data for various pcs in my network... Snap of the file is as follows ******************************************************************************* Serial 123456 Computer IP Address lo0:... (1 Reply)
Discussion started by: aavam
1 Replies

10. Shell Programming and Scripting

How to insert data befor some field in a row of data depending up on values in row

Hi I need to do some thing like "find and insert before that " in a file which contains many records. This will be clear with the following example. The original data record should be some thing like this 60119827 RTMS_LOCATION_CDR INSTANT_POSITION_QUERY 1236574686123083rtmssrv7 ... (8 Replies)
Discussion started by: aemunathan
8 Replies
Login or Register to Ask a Question