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

1
2
3
4
5

1.txt file look like this:

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

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

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:

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

Then I multiply the numbers like this:

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.

Try this...

for num in `seq 1 200000`; do  
  paste -d"*" $num.dat $num.txt | bc >$num.mul 
done

--ahamed

1 Like

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:

find -maxdepth 1 -type f -size 0 -print0 | xargs -0 rm -f
for num in `seq 1 200000`; do
  test ! -f $num.dat && continue
  paste -d"*" $num.dat $num.txt | bc >$num.mul 
done

--ahamed

1 Like
for file in *.dat
do
  pre=`echo ${file%%.*}`
  awk -v f=$pre.txt '{getline a<f;print a*$1}' $file > $pre.mul
done
1 Like

ksh93, no external programs:

#!/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
1 Like