Multiply numbers from different files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiply numbers from different files
# 1  
Old 10-03-2011
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 names like (1.txt, 2.txt, 5.txt, 8.txt.....200000.txt)

As you can see for every .dat with number there is a corresponding .txt...that is 1.dat has 1.txt, 2.dat has 2.txt and this thing goes on.

.dat files look like this:

For example, 1.dat

Code:
1
2
3
4
5

1.txt file look like this:
Code:
4.5
0.34
3.45
9
5.67

Another thing is that the number of lines in 1.dat and 1.txt are all same too.

What I need to do is to multiple the corresponding FILES with corresponding numbers in each file and store the result in .mul file. That is, multiply numbers present in 1.txt with 1.dat line by line and store the result in 1.mul and same thing goes for 2.txt and 2.dat with creating of 2.mul.

This is what I need to do in order to create 1.mul
Code:
4.5	* 1	= 4.5
0.34	* 2	= 0.68
3.45  * 3	= 10.35
9	* 4	= 36
5.67	* 5	= 28.35

So, my 1.mul will have


Code:
4.5
0.68
10.35
36
28.35

This is how I do for every file.

Now, the problem that I am facing is that my file numbers are not in continuity. As you can see after 2.dat; 5.dat comes in. So, my script breaks.

I am trying to do this:

First I paste values together:

Code:
for num in `seq 1 200000`; do
 paste $num.dat $num.txt >$num.common
done

Then I multiply the numbers like this:
Code:
ls -1 *.common | while read page
do
cat $page | awk '{print $1 * $2}' $page>$page.txt
done

But I believe I am using unnecessary disk space by creating .common files. I am working on Linux with BASH.
# 2  
Old 10-03-2011
Try this...
Code:
for num in `seq 1 200000`; do  
  paste -d"*" $num.dat $num.txt | bc >$num.mul 
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 10-03-2011
Well, thanks. Though it is still creating files like 3.mul, 4.mul for which no 3.dat or 3.txt exists but I can do with that. I just have to remove those zero length files with this command:

Code:
find -maxdepth 1 -type f -size 0 -print0 | xargs -0 rm -f

# 4  
Old 10-03-2011
Code:
for num in `seq 1 200000`; do
  test ! -f $num.dat && continue
  paste -d"*" $num.dat $num.txt | bc >$num.mul 
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 10-04-2011
Code:
for file in *.dat
do
  pre=`echo ${file%%.*}`
  awk -v f=$pre.txt '{getline a<f;print a*$1}' $file > $pre.mul
done

This User Gave Thanks to rdcwayx For This Post:
# 6  
Old 10-04-2011
ksh93, no external programs:
Code:
#!/bin/ksh93
for f in *.dat; do
  while read i; read j <&3; do
    echo "$(( i*j ))"
  done <"$f" 3<"${f%.dat}.txt" >"${f%.dat}.mul"
done


Last edited by Scrutinizer; 10-04-2011 at 02:32 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Multiply value by row

Hi I would like to know how can I multiply the value of column three to columns 4-end of file example of input file: rs1000012 AK8 2 0.05 0.05 1 0 0 rs10000154 PAQR3 0.01 2 1 2 2 1 Desired output file: rs1000012 AK8 ... (1 Reply)
Discussion started by: fadista
1 Replies

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

3. Shell Programming and Scripting

awk multiply values contained in 2 different files

Hi Everyone ! I have two files with the same configuration and I want to multiply corresponding values and write the result in a file. Let say 2 header lines and then lines of values (with not constant number of columns): more file1.txt --> BLABLABLA BLABLABLA 1 2 3 4 1 2 3 1 2 1... (7 Replies)
Discussion started by: Youm
7 Replies

4. Shell Programming and Scripting

multiply with awk

HI help i have cc 9+37.50 328611.50 688498.25 42.38 cc 66+62.50 328636.50 688498.42 42.58 i want to make o/p cc 9+3750 328611.50 688498.25 42.38 cc 66+6250 328636.50 688498.42 42.58 plz help (2 Replies)
Discussion started by: Indra2011
2 Replies

5. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

6. Shell Programming and Scripting

Multiply whole column with a value

Hi, I need to multiply 3rd column (comma seperated) of entire file by a value say 2.2. Suppose the file is: C,Gas $ YTD(TRI),15512.36,01/01/2010 New file should be (3rd column value multiplied by 2.2): C,Gas $ YTD(TRI),34127.192,01/01/2010 (5 Replies)
Discussion started by: yale_work
5 Replies

7. Shell Programming and Scripting

All I want to do is Multiply...

All I want to do is find 5!. read num num={1..5} while do f= done echo f Error Msg. line 5: ${1..5} bad substitution line 6: integer expression expected Line 5 is the num=... Line 6 is the "while" statement I am new at this, and I am really, really trying. Please... (14 Replies)
Discussion started by: Ccccc
14 Replies

8. Shell Programming and Scripting

Multiply variables

I have no idea about programming, just know some simple html :confused:and I need to get to somebody that can help me with creating the application (?) that will multiply 2 varibales and given price (height x lenght) x $$$. PLEASE HELP!:confused: edit by bakunin: Anita, as much as we... (2 Replies)
Discussion started by: Anita Flejter
2 Replies

9. Shell Programming and Scripting

multiply variable

I need to multiply the value of a variable and then store it in another variable. I have EXPHOURINSEC=$(($EXPDATEHOUR * 3600)) but i get an error saying the * is unexpected. Im using ksh (4 Replies)
Discussion started by: fwabbly
4 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