Home > Software design >  Sum numbers in multiple different files
Sum numbers in multiple different files

Time:03-29

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!

CodePudding user response:

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.

CodePudding user response:

Read one line from each file and join them with a delimiter, this is one of the things paste(1) does well. Pass the result on to bc(1) to get the sums:

paste -d  file1 file2 file3 | bc -l

Output:

1.6
3.5
  • Related