summing numbers in files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting summing numbers in files
# 1  
Old 07-31-2008
summing numbers in files

I am trying to take two files and add the numbers from each. There is a total of 5192 numbers in each file and I want to add them row by row... ie. first row of file 1 + first row of file 2 = first row of output. Eventually I will be summing 40401 of these files together but starting with 2 just for tests. This is what I have so far:

Code:
#!/bin/csh
#
set a = `more blankdata.txt`
@ n = 0

while( $n < 2 )
    @ counter = 1
    set m = `more out.$n`

    while( $counter < 5193 )
    
    set a[$counter] = `echo $a[$counter]+$m[$counter]|bc`
    @ counter ++
    end

    @ n ++

end

If I run this I get the following error:
"(standard_in) 1: illegal character: |
(standard_in) 1: parse error"

Any help is appreciated.
# 2  
Old 07-31-2008
Tools Off the top of my head....

Note: not sure of all syntax; not in front of a unix system to test (and too tired to connect to the server!)

Code:
counter=0
while read zf
   do
   counter=$((counter+1))
# may want to adjust this
   val1=$(echo "$zf")
   val2=$(cat file2 | head -"$counter" | tail -1)
   val3=$((val1+val2))
   echo "$val3" >>file3
done <file1

# 3  
Old 07-31-2008
awk should do that:
Code:
awk 'NR==FNR{a[NR]=$1;next}{print (a[FNR]+$1)}' file1 file2 > output

# 4  
Old 07-31-2008
Try this:

Code:
awk '{tot[FNR]+=$1} FNR>max {max=FNR} END {for (i=1;i<=max;i++) { print tot[i] }}' file1 file2 ...

You may run into a "number of arguments" limit if you try and do all 5192 files at once though... see how it goes.
# 5  
Old 07-31-2008
hey guys, these both work but they both cut off the numbers after the first number after the decimal point(some go to 13 decimal places). I need most of these numbers in full, is this an easy fix?

Code:
awk '{tot[FNR]+=$1} FNR>max {max=FNR} END {for (i=1;i<=max;i++) { print tot[i] }}' file1 file2 ...
awk 'NR==FNR{a[NR]=$1;next}{print (a[FNR]+$1)}' file1 file2 > output

# 6  
Old 07-31-2008
How about this instead?

Code:
paste all your files here | awk '{ s=0; for (i=1; i<=NF; ++i) s+=$i; print s }

(Just to be painfully clear, paste is the name of a command, and all your files here is a placeholder for the names of the files you want to sum.)
# 7  
Old 07-31-2008
ends up giving the same numbers.... for all three methods some of the numbers do go to the second decimal place but not all of them

any other ideas?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Summing up values of rows of numbers

data file contains failed=24 error=23 error=163 failed=36 error=903 i need to get a total count of each value above. i'm looking for the most efficient method to do this as the datafile i provided is just a sample. the actual data can be several hundred thousands of lines. so from... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Sum of numbers in three or more files

I have files : cat file1 15 88 44 667 33 4cat file2 445 66 77 3 56 (12 Replies)
Discussion started by: Natalie
12 Replies

3. Shell Programming and Scripting

How to take a Average of numbers from different files?

Hi, I have 3 to 4 different files, from that i need to take a Average of numbers from a particular column. here i have to take 4th column, that should present in diff. file. File 1: Col1 col2 col3 col4 1 11 sa 12.00 2 22 sb 134.59 3 33 sc 11.99 4 44 sd 12.44 Col1 col2 col3... (8 Replies)
Discussion started by: Shenbaga.d
8 Replies

4. Shell Programming and Scripting

summing from two different files

I have two files hhhh 3674.00 a 75 1535 183 2134 291 2452 442 2738 704 3048 a 1007 3549 1282 4413 1494 5001 1631 5217 1954 5610 a 2540 5832 3248 6080 3629 6264 4851 6600 7004 6985 ... (4 Replies)
Discussion started by: Indra2011
4 Replies

5. Shell Programming and Scripting

Multiply numbers from different files

Hi All, I have tried few things with this but it did not help much. I have some 200,000 files in a directory. There are two sets of files. 1. Files with extension .dat with file names like these (1.dat, 2.dat, 5.dat, 8.dat....200000.dat) 2. Another set of files with .txt extension and... (5 Replies)
Discussion started by: shoaibjameel123
5 Replies

6. Shell Programming and Scripting

Summing numbers after specific word

Hi all, Looking for suggestions on a better way to sum numbers in a key value pair formated file. What I have works but seems really clunky to me. Any suggestions would be greatly appreciated. cat test.txt | perl -ne 'm/(M=)(\d+\.?\d?\d?)/ && print "$2\n"' | awk '{ sum+=$1} END {printf... (7 Replies)
Discussion started by: cgol
7 Replies

7. Shell Programming and Scripting

Sum Numbers from different files

Hi All, I need to print the sum of numbers from different files. Input files: file1.out 10 20 30 file2.out 10 20 30 (5 Replies)
Discussion started by: saint2006
5 Replies

8. Shell Programming and Scripting

Reading several files and summing their content line-by-line

Hey, I am gettin a bit crazy with my script. I have several input datas with the same name (5.ill) in different folders (daysim_01, daysim_02, etc.). The 4. column of each of the data has to be summed with each other and then hass to be written in one new file. So file1: 1 1 0 1 2 1 1 2 ... (7 Replies)
Discussion started by: ergy1983
7 Replies

9. UNIX for Dummies Questions & Answers

To get unique numbers from two files

here i have two files: file 1 1 2 3 4 5 5 6 7 8 9 file 2 4 5 6 6 8 8 (6 Replies)
Discussion started by: i.scientist
6 Replies

10. Shell Programming and Scripting

altering numbers in files

I want to change a number in a file into number -1.. for instance file_input is fdisdlf_s35 fdjsk_s27 fsdf_s42 jkljllljkkl_s57 ... etc now i want the output to be fdisdlf_s34 fdjsk_s26 fdsf_s41 jkljllljkkl_s56 ... etc I was think of using "sed -e 's/2/1/g' -e 's/3/2/g' -e... (4 Replies)
Discussion started by: bigboizvince
4 Replies
Login or Register to Ask a Question