sum same column across multiple files using awk ? -
i want add 3rd column of 5 files such new file have same 2nd col , sum of 3rd col of 5 files.
i tried this:
$ cat freqdat044.dat | awk '{n=$3; getline <"freqdat046.dat";print $2" " n+$3}' > freqtrial1.dat freqdat048.dat`enter code here`$ cat freqdat044.dat | awk '{n=$3; getline <"freqdat046.dat";print $2" " n+$3}' > freqtrial1.dat
the files names:
freqdat044.dat freqdat045.dat freqdat046.dat freqdat047.dat freqdat049.dat freqdat050.dat
and saved in output file contain $2
, new col form summation of 3rd
awk '{x[$2] += $3} end {for(y in x) print y,x[y]}' freqdat044.dat freqdat045.dat freqdat046.dat freqdat047.dat freqdat049.dat freqdat050.dat
this not print lines appear in first file. if want preserve sorting, have save ordering somewhere:
awk 'fnr==nr {keys[fnr]=$2; cnt=fnr} {x[$2] += $3} end {for(i=1; i<=cnt; ++i) print keys[i],x[keys[i]]}' freqdat044.dat freqdat045.dat freqdat046.dat freqdat047.dat freqdat049.dat freqdat050.dat
Comments
Post a Comment