Simple way to add columns from multiple files Simple way to add columns from multiple files bash bash

Simple way to add columns from multiple files


You can use this awk:

awk '{a[FNR]=$1; s[FNR]+=$2} END{for (i=1; i<=FNR; i++) print a[i], s[i]}' file1 file2# 0.71.0 0.11.5 1.62.0 2.12.5 1.5

FNR starts from 1 for each file so you can pass all of your input files to this awk command.


I don't see why you think the paste/awk solution is unfeasible. Here is one way you could do it, assuming each file only has 2 columns:

paste * | awk '{ s=$2; for(i=4; i<=NF; i+=2) s+=$i; print $1, s }'

Output:

1.0 0.11.5 1.62.0 2.12.5 1.5