Multiply numbers in two columns and then add.

I have some 100 files with extension .tmp The files are named as 1.tmp, 2.tmp, 3.tmp until 100.tmp

All files look like this:

0.38701788 1.968068e-02
0.38622013 2.054002e-02
0.38350296 1.715522e-02
0.38282126 1.781283e-02
0.38282126 1.781283e-02
0.35847232 1.026839e-02
0.3557739 8.556013e-03
0.35375914 1.318722e-02
0.35018 1.884645e-02
0.3486718 9.611522e-03

I want to multiply each line with two different numbers and then add the result to store the result in a separate file.

For example, this is what I need to do:

0.38701788*0.3 +1.968068e-02*0.5
0.38622013*0.3 + 2.054002e-02*0.5
0.38350296*0.3 + 1.715522e-02*0.5
0.38282126*0.3 + 1.781283e-02*0.5
0.38282126*0.3 + 1.781283e-02*0.5
0.35847232*0.3 + 1.026839e-02*0.5
0.3557739*0.3 + 8.556013e-03*0.5
0.35375914*0.3 + 1.318722e-02*0.5
0.35018*0.3 + 1.884645e-02*0.5
0.3486718*0.3 + 9.611522e-03*0.5

I want to store the result of each file line by line in *.res file.

I am not sure whether I am doing this correctly because the second column has exponential numbers. I am using Linux.

for num in `seq 1 100`; do
number1=awk '{$1=""}1' * 0.3
number2= awk '{$2=""}1' *.5
number1+number2 >$num.res
done

My output for above case should be like this, but I am not getting that:

0.021450876
0.021856614
0.020082699
0.020391053
0.020391053
0.015888365
0.014951224
0.017206384
0.019928625
0.015265915

Are you sure that 0.38701788*0.3 +1.968068e-02*0.5 calculates to 0.021450876 ?
Please check and let us know.

--ahamed

I am sorry, my mistake:

0.125945704
0.126136049
0.123628498
0.123752793
0.123752793
0.112675891
0.111010177
0.112721352
0.114477225
0.109407301
for infile in *.tmp
do
   outfile=${infile%.*}.res
   awk '{ print $1*0.3 + $2*0.5 }' $infile > $outfile
done
1 Like

deleted

--ahamed

It cannot be 0.24. lets take only until 2 decimal places.

0.38*0.3=0.114
0.01*0.5=0.005

now add = 0.119

I was calculating it wrong. My bad!

--ahamed

1 Like

Never Mind :slight_smile: but thanks for your help