Another, more memory-efficient option could be to use paste to get all the relevant lines from each file together:
% paste -d '\n' file*.dat 1 1 3 0 1 3 4 8 9 0 5 9 10 11 3 9 2 4 And then use awk on them:
# cat rowsum-paste.awk NR > 1 && NF != prevNF { for (i = 1; i <= prevNF; i++) { printf "%s ", sum[i]; sum[i] = 0 }; printf "\n" } { for (i = 1; i <= NF; i++) sum[i] += $i; prevNF = NF } % (paste -d '\n' file*.dat; echo) | awk -f rowsum-paste.awk 4 1 9 12 4 8 18 12 15 This awk code sums lines until the number of fields changes, and then prints and resets the current sums. The extra echo is to change the number of fields at the end and trigger the final print, which can also be done with the printing code duplicated in an END block.