how to sum values from 2 different files?

Hi

I am trying to add count values from two different files into one file. Could any body please suggest me best command to do this? My problem was as follows:

a.txt b.txt c.txt
10 20 30(needed)

i tried cat a.txt b.txt > c.txt (its not adding the values)

Thanks in advance..
Praveen

What do you mean with "count values"?

not sure what you're looking for. Please post a sample input file(s) and a desired output using the vB Codes.

Also you might want to take a look at 'man paste' (if I guessed your intentions correctly).

read a < a.txt
read b < b.txt
printf "%d\n" $(( $a + $b )) > c.txt

Hi,

Don't misunderstand about "Counts", actually i have two flat files which contains only one numeric values, i want to add both the values and store the results into third file.

file1.txt file2.txt file3.txt

 10             20                 file1.txt_value\(10\) \+ file2.txt_value \(20\)

Thanks!
Praveen

Hi Johnson,

Your code is working. Can you please suggest me how to change the existing script to dynamic script with parameters, such that it take three file names as 3 parameters and add the content of first two files and insert the sum value into third file? Your help will be appreciated.

script(file1name, file2name, file3name)

{

body of script

file 3 should contain sum of file1 content and file2.

}

Thanks!
Praveen

Try:

paste file1 file22 | awk '{ print $1+$2; }' >file3
## NAME: fsub - subtract numbers in FILE1 from FILE2
## USAGE: fsub FILE1 FILE2 DESTFILE
paste "$1" "$2" | awk '{ print $2 - $1 }' > "$3"
nawk '{
if(NR==FNR)
	_[NR]=$0
else
	print $0+_[FNR]
}' a.txt b.txt