bash - Calculating the sum of every third column from many files -
i have many files 3 columns in form of:
file1 | file2 1 0 1 | 1 0 2 2 3 3 | 2 3 7 3 6 2 | 3 6 0 4 1 0 | 4 1 3 5 2 4 | 5 2 1
first 2 columns same in each file. want calculate sum of 3 columns every file receive this:
1 0 3 2 3 10 3 6 2 4 1 3 5 2 5
for 2 files awk 'fnr==nr { _a[fnr]=$3;} nr!=fnr { $3 += _a[fnr]; print; }' file*
work (i found solution via google). how change on many files?
since first 2 columns same in each file:
awk 'nr==fnr{b[fnr]=$1 fs $2;}{a[fnr]+=$3}end{for(i=1;i<=length(a);i++){print b[i] fs a[i];}}' file*
array a
used have cumulative sum of 3rd column of files. array b
used 1st , 2nd column values in end, print contents of array a
, b
Comments
Post a Comment