Sum number in two different files

I have two files of the same length:

0.99952 0.01 0.98927 0.02 0.98242 0.03 

and

 -0.758 0.01 -0.745 0.02 -0.742 0.03 

I would like to summarize the first column numbers and print them in a new file with the same elements as in the second column. The output should be:

 0.24152 0.01 0,24427 0.02 0,24042 0.03 

I tried using

  paste file1 file2 | awk '{print ($1 + $2), $4}' > sum 

but conclusion:

  1.00952 0.01 1.00927 0.02 1.01242 0.03 
+4
source share
2 answers

You accept an invalid parameter number in awk ($ 2 instead of $ 3)

 $1 $2 $3 $4 0.99952 0.01 -0.758 0.01 0.98927 0.02 -0.745 0.02 0.98242 0.03 -0.742 0.03 

There is a working example:

 paste data1 data2 | awk '{print ($1 + $3), $4}' 
+6
source

The only awk approach:

 $ awk 'NR==FNR{a[NR]=$1;next}{print $1+a[FNR],$2}' file1 file2 0.24152 0.01 0.24427 0.02 0.24042 0.03 
+5
source

All Articles