Multiply numbers in two columns and then add.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiply numbers in two columns and then add.
# 1  
Old 10-28-2011
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:

Code:
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:

Code:
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.

Code:
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:

Code:
0.021450876
0.021856614
0.020082699
0.020391053
0.020391053
0.015888365
0.014951224
0.017206384
0.019928625
0.015265915

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

--ahamed

Last edited by ahamed101; 10-28-2011 at 02:10 AM..
# 3  
Old 10-28-2011
I am sorry, my mistake:


Code:
0.125945704
0.126136049
0.123628498
0.123752793
0.123752793
0.112675891
0.111010177
0.112721352
0.114477225
0.109407301

# 4  
Old 10-28-2011
Code:
for infile in *.tmp
do
   outfile=${infile%.*}.res
   awk '{ print $1*0.3 + $2*0.5 }' $infile > $outfile
done

This User Gave Thanks to fpmurphy For This Post:
# 5  
Old 10-28-2011
deleted

--ahamed
# 6  
Old 10-28-2011
It cannot be 0.24. lets take only until 2 decimal places.

Code:
0.38*0.3=0.114
0.01*0.5=0.005

now add = 0.119
# 7  
Old 10-28-2011
Quote:
Originally Posted by shoaibjameel123
It cannot be 0.24. lets take only until 2 decimal places.

Code:
0.38*0.3=0.114
0.01*0.5=0.005

now add = 0.119
I was calculating it wrong. My bad!

--ahamed
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Search for columns with numbers greater than 70

This helped get me started. https://www.unix.com/unix-for-dummies-questions-and-answers/157026-need-command-search-numbers-greater-than-3000-a.html This is the command I am using. I am trying to find numbers greater than 70 in column 5. I do not know if it is getting confused because there... (6 Replies)
Discussion started by: cokedude
6 Replies

2. UNIX for Beginners Questions & Answers

Add column and multiply its result to all elements of another column

Input file is as follows: 1 | 6 2 | 7 3 | 8 4 | 9 5 | 10 Output reuired (sum of the first column $1*$2) 1 | 6 | 90 2 | 7 | 105 3 | 8 | 120 4 |9 | 135 5 |10 | 150 Please enclose sample input, sample output, and code... (5 Replies)
Discussion started by: Sagar Singh
5 Replies

3. Shell Programming and Scripting

awk - Adding and Subtracting Numbers from 2 Columns

Hi Folks, I have a file with 2 columns TAB delimited and I want to add '1' to the first column and subtract '-1' from the second column. What I have tried so far is; awk -F"\t" '{ $1-=1;$2+=1}1' OFS='\t' file File 0623 0623 0624 0624 0643 0643 1059 1037 1037 1037 1038 1038... (2 Replies)
Discussion started by: pshields1984
2 Replies

4. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

5. Shell Programming and Scripting

Multiply a row of numbers

the following is used to add numbers: echo 7 47 47 44 4 3 3 3 3 3 | awk '{ for(i=1; i<=NF;i++) j+=$i; print j; j=0 }' how do i multiply OR subtract a row of numbers using the above tactic? (8 Replies)
Discussion started by: SkySmart
8 Replies

6. UNIX for Dummies Questions & Answers

Urgent_need to delete columns with numbers

Dear all, I have one file (see below) with more then 100 columns, and need only column which has GType in label with Alphabets, please help me to remove this columns with numbers. input file is n.201.GType n-201.Theta n-201.R n_1.GType n_1.Theta n_1.R n_7.GType ... (1 Reply)
Discussion started by: AAWT
1 Replies

7. 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

8. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

9. Shell Programming and Scripting

Reversing numbers in a datafile of rows and columns

Hello, I've tried searching the forum for an answer to my question, but without any luck... I have a datafile looking simplified as follows: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 I want to reverse it by rearranging all the numbers from last to... (16 Replies)
Discussion started by: mattings
16 Replies

10. UNIX for Dummies Questions & Answers

How to add/multiply numbers with scientific notation (2.343e-5)

Hi, I'm need to do some addition and multiplication of scientific nottaion numbers, in the form 34.23423e-10 for example. I was echoing the list of numbers to stdout, then using bc -l, then I find that this does not seem to work for numbers with exponential notation. Could someone help me out... (1 Reply)
Discussion started by: chugger06
1 Replies
Login or Register to Ask a Question