# sum numbers in multiple files

**URL:** <https://community.unix.com/t/sum-numbers-in-multiple-files/205674>\
**Category:** Shell Programming and Scripting\
**Created:** [July 21, 2008, 2:23pm UTC](https://community.unix.com/t/sum-numbers-in-multiple-files/205674 "2008-07-21T14:23:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pattywac](https://community.unix.com/letter_avatar/pattywac/32/6_22734026db12803ebb3e059622ac3534.png) [@pattywac](https://community.unix.com/u/pattywac)\
**Post date:** [July 21, 2008, 2:23pm UTC](https://community.unix.com/t/sum-numbers-in-multiple-files/205674/1 "2008-07-21T14:23:10Z")

</div>

I have 11 directories with around 200 files in each. In each directory the files are labeled out.0 through out.201 . Each file has around 118 numbers in a single column. I need to sum the files in each directory so each directory will have a resultant vector that is 118 numbers long. I then need to combine the vectors into a single file that is 11\*118 (1298) numbers long. Someone mentioned using 'cat' but since i only wrote my first csh script over the weekend I have no idea where to start.

thanks in advance

---

<div class="post-metadata">

**Author:** ![joeyg](https://community.unix.com/user_avatar/community.unix.com/joeyg/32/780_2.png) [@joeyg](https://community.unix.com/u/joeyg)\
**Post date:** [July 21, 2008, 3:37pm UTC](https://community.unix.com/t/sum-numbers-in-multiple-files/205674/2 "2008-07-21T15:37:58Z")

</div>

```nohighlight
DirA
 out.0
  contains 118 rows, each with a number
 out.1
  contains 118 rows, each with a number
 ...
DirB
 repeats

```

Fairly straight-forward to hav a loop add up all of the numbers.  
But, not sure what you mean by resulting vector.

---

<div class="post-metadata">

**Author:** ![pattywac](https://community.unix.com/letter_avatar/pattywac/32/6_22734026db12803ebb3e059622ac3534.png) [@pattywac](https://community.unix.com/u/pattywac)\
**Post date:** [July 21, 2008, 3:47pm UTC](https://community.unix.com/t/sum-numbers-in-multiple-files/205674/3 "2008-07-21T15:47:38Z")

</div>

you have the directory structure right. Im not trying to sum up each number within the file. Within each dir all 200 of the first entries would be summed, same for the second ..... through the 118th entry of each file. This would result in each directory having a summed column that is 118 numbers long. I then need to combine those numbers from each dir into one file so it would contain the 118 numbers \* 11 dir = 1298 numbers in the final data file.

so inside a dir:

out.1:  
0  
5  
10

out.2  
0  
5  
100

sum=  
0  
10  
110

---

<div class="post-metadata">

**Author:** ![joeyg](https://community.unix.com/user_avatar/community.unix.com/joeyg/32/780_2.png) [@joeyg](https://community.unix.com/u/joeyg)\
**Post date:** [July 21, 2008, 4:34pm UTC](https://community.unix.com/t/sum-numbers-in-multiple-files/205674/4 "2008-07-21T16:34:36Z")

</div>

```nohighlight
DirA
 out.0
  contains 118 rows, each with a number
 out.1
  contains 118 rows, each with a number
 ...
DirB
 repeats

```

```nohighlight
inside DirA
 open out.0
  add up all rows in the file, get a number 31543
  write that to a temp file call DirA_sums
 open out.1
  add up all rows in the file, get a number 19548
  write that to a temp file call DirA_sums
  DirA_sums now contains 31543 19548
 continue through all of the files
inside DirB
 process the out.? files in same manner

continue through DirK (11th directory)

```

After the above, I would have eleven files, each containing 118 numbers (which are the sums from the files within the directory)

Do I got that right?

---

<div class="post-metadata">

**Author:** ![pattywac](https://community.unix.com/letter_avatar/pattywac/32/6_22734026db12803ebb3e059622ac3534.png) [@pattywac](https://community.unix.com/u/pattywac)\
**Post date:** [July 21, 2008, 4:46pm UTC](https://community.unix.com/t/sum-numbers-in-multiple-files/205674/5 "2008-07-21T16:46:10Z")

</div>

not quite.....  
should be more like this:

```nohighlight
inside DirA
 open out.0
  store all rows in the file to array
 open out.1
  store all rows in the file to temparray
  set array = array + temparray
 open out.2
  store all rows in the file to temparray
  set array = array + temparray

 continue through all of the files
 Write array to tempfile out.DirA

inside DirB
 process the out.0-200 files in same manner

continue through DirK (11th directory)

after 11th directory
Write out.DirA to finaloutput.file
append out.DirB to finaloutput.file
...
append out.DirK to finaloutput.file

```

---

<div class="post-metadata">

**Author:** ![joeyg](https://community.unix.com/user_avatar/community.unix.com/joeyg/32/780_2.png) [@joeyg](https://community.unix.com/u/joeyg)\
**Post date:** [July 21, 2008, 5:21pm UTC](https://community.unix.com/t/sum-numbers-in-multiple-files/205674/6 "2008-07-21T17:21:30Z")

</div>

given a sample structure (2 dirs, 4 files in each)  
each file has five numbers in it, one per line  
./DirA/out.0  
./DirA/out.1  
./DirA/out.2  
./DirA/out.3  
./DirB/out.0  
./DirB/out.1  
./DirB/out.2  
./DirB/out.3

Start of the script programming

```nohighlight
> cat manip  
#! /usr/bin/bash

find . -name out* | sort >file_list

rm *.arr 2>/dev/null
while read zf
  do
  echo $zf
  tfile=$(echo $zf | cut -d "/" -f2)
  arr_txt=$(cat $zf | tr "\n" " ")
  echo "$arr_txt" >>"$tfile".arr
done <file_list

```

program execution

```nohighlight
> manip
./DirA/out.0
./DirA/out.1
./DirA/out.2
./DirA/out.3
./DirB/out.0
./DirB/out.1
./DirB/out.2
./DirB/out.3

```

see the resultant two files created

```nohighlight
> cat DirA.arr
2 4 6 8 10  
1 3 5 7 9  
21 22 31 32 33  
12 13 14 15 16  
> cat DirB.arr
3 4 5 6 7  
6 7 19 10 11  
5 10 9 6 14  
19 20 2 3 10  

```

I know that the files were read properly, to create each line in my .arr files.

So, if this looks to be on-course, it is fairly straight-forward to combine my .arr files into one larger file. So...
