Search code examples
bashawksum

Sum numbers in multiple different files


I'm looking for the most efficient way to sum X columns of floats, each column is stored in a distinct file.

All files have exactly the same number of lines (a few hundred). I do not know in advance the number X.

Example with X=3:

File1:

0.5
0
...

File2:

0
1.5
...

File3:

1.1
2
...

I'd like to generate a file, say sum_files:

1.6
3.5
...

Any efficient way to do this in awk or bash? (There exist solutions using adhoc python scripts, but I'm wondering how this can be done in awk or bash.)

Thanks!


Solution

  • I would harness GNU AWK's FNR built-in variable for this task following way:

    awk '{arr[FNR]+=$1}END{for(i=1;i<=FNR;i+=1){print arr[i]}}' file1 file2 file3
    

    Explanation: for each line do increase value in array arr under key being number of line in file by value of 1st field. After processing all files print values stored in arr. Note that FNR might be used in for as limit due to fact that all files have equal number of lines.