Bash – How to add numbers from two txt files with Bash

bashnumeric datashell-scripttext processing

I have a txt file that contains some numbers like this:

1  
2  
3  
4  
5  

And I have another txt file that contains the same number of lines, but with other numbers:

6  
7   
8  
9  
10

I want to add them together, namely 1+6, 2+7, 3+8, etc.. How do I write the script?

By the way, I've got a variety of answers so far, and only after I tried them on my files did I realise some of the methods can't deal with decimals. Some of my files contain decimals, and I need to be accurate, so if you would like to add an answer, could you show a method that can calculate decimals accurately. Thanks.

Best Answer

This is basic task many tools can solve; paste + awk combo seems exceptionally handy:

$ paste file1 file2 | awk '{$0=$1+$2}1'
7
9
11
13
15
Related Question