need [HELP] sum array multiple files

Hi.. I'm very newbie here..

I wonder if somebody can help me..

I have multiple directories with same out file name for each directories..

./dirA/out.dat
./dirB/out.dat
./dirC/out.dat
..and so on..

for ./dirA/out.dat here is the structure content :

for ./dirB/out.dat the same structure but different data :

and so on for other files..

What I want to do is to sum the data in the same cell for every files..

so for example above my expected output file is :

How can I make the script using awk ..? or I should make fortran code..? I have no idea to do both.. :frowning:

Thank you in advance..

Best Regards..
--- agiantz

which data to be added..? i mean "What SUM" you need?

21+ 43+ 70+ 70 = *

or 21 + 70 = * ???

the last example is the result that I want..
21 43 70 70 is the result

I want to sum from several files that have same dimension,
example :
file 1 :
1 10 20 30 40
2 50 60 50 10
3 ... (first coulomb just number, not the data)
+ file 2 :
1 11 23 40 30
2 20 30 10 50
3 .. (and so on with other files..)

so i will have output file like this :
1 21 43 70 70
2 70 90 60 60
3 ...
(where 21 is sum from 10 in the first file with 11 in 2nd file..)
the position for every cell is the same..

Browse before posting.

I googled like this:

shell get sum from two files

and i got a closed post on 07-21-08.

http://www.unix.com/shell-programming-scripting/74192-sum-numbers-multiple-files.html

.

Try this little Perl script:

#!/usr/bin/perl
chomp(@files=`find . -name "out.dat"`);
for $f (@files){
  open F, "$f";
  chomp(@rows=<F>);
  for $r (0..$#rows){
    @cells=split / /,$rows[$r];
    for $c (0..$#cells){
      $sum[$r][$c]+=$cells[$c] if $c>0;
      $sum[$r][$c]=$cells[$c] if $c==0;
    }
  }
}
for $r (0..$#rows){
  print "@{$sum[$r]}\n";
}

Save it in the directory containing dirA, dirB etc. Then run it without arguments: ./script.pl

1 Like

I already try that, but the problem is different because I need sum only for every cells in same dimension..

You are my savior.. :slight_smile:
Thank you very much.. it's work like magic.. :slight_smile:

Save my time for my hundreds of data....

Arigatou gozaimasu...

awk '{i=1;A[FNR":"i]=$1
        do {x=FNR":"(++i)
                A[x]+=$i
        }while(i<NF)
}END{f=1;do {
        k=1;do{
                y=(y?y OFS:z) A[f":"k]
        }while(++k<=NF)
        print y;y=z
}while(f++<FNR)}' dir*/out.dat

---------- Post updated at 10:50 AM ---------- Previous update was at 10:45 AM ----------

$ cat dirA/out.dat
1 10 20 30 40
2 50 60 50 10
$ cat dirB/out.dat
1 11 23 40 30
2 20 30 10 50
$ cat my.awk
{i=1;A[FNR":"i]=$1
        do {x=FNR":"(++i)
                A[x]+=$i
        }while(i<NF)
}END{f=1;do {
        k=1;do{
                y=(y?y OFS:z) A[f":"k]
        }while(++k<=NF)
        print y;y=z
}while(f++<FNR)}
$ awk -f my.awk OFS="\t" dir*/out.dat
1       21      43      70      70
2       70      90      60      60
$

You could even choose the Output Field Separator

1 Like