Search code examples
shelladditionlines

Add the values of each line of only one column from many files with 2 columns - shell scripting


I have many files that have this structure that have two columns of numbers. And I want to add each line value of the second column, for all of my files, so I'll end up with only one file. Can anyone help? Hope the question was clear enough. Thanks.


Solution

  • The following is based on the information OP provided in his comments here above:

    1. We have multiple files and we have to sum the second column of each of these files. As far as we know we could have hundreds or thousands of different files
    2. The first column in each file seems not important and I'm going to assume (based on OP sample data) we have the same (first) column in each input file

    The basic idea is to start with an empty summary (file tot), paste one after the other each file with tot and sum 2 and 4 columns (if present) into the second column of the new tot file.

    In other words...

    $ touch tot ; for f in * ; do paste tot ${f} | awk '{ if ( NF > 3 ) { print $1, $2+$4 } else { print $1, $2 } }' > tmp ; mv tmp tot ; done 
    

    I did test it with 8 different files and seems to work as expected. Of course for f in * has to be changed in order to capture ALL and ONLY the files we want to sum.