Multiplication of a column from 2 files using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiplication of a column from 2 files using awk
# 1  
Old 08-13-2013
Multiplication of a column from 2 files using awk

Hi,

I have two files (file1.txt, file2.txt) in which, I would like to multiply all the values in file1 with the first row value of file2, file1 * second row value of file2, file1 * third row value of file2 and so on.

Below are my sample data.
Code:
file1.txt   file2.txt                  desired outputs   
2              2          out1.txt (file1.txt*2)   out2.txt(file1.txt*3)     
3              3                 4                     6 
4              4                 6                     9    
6                                8                     12
8                                12                    18 
                                 16                    24
 and so on

Thank you very much for any help.
# 2  
Old 08-13-2013
What have you tried?
# 3  
Old 08-13-2013
Hi,

I tried something like this but it doesnt even get past the first line. Smilie

Code:
for i in {1..3}
      do awk 'NR==$i{print $1}' file2.txt > $i.tmp
           paste file1.txt  $i.tmp | awk '{print $1*$2}' > "out$i.txt"
done

Thanks.
# 4  
Old 08-14-2013
The for i in {1..3} works in some shells, but not others. Since the first line of your script doesn't specify what shell you want, it will run using your system's default shell. Then $i in your awk command will not be an expansion of your shell variable; it will reference $0 (the entire input line) since the variable i in your awk script is undefined.
You could try something like:
Code:
awk '
FNR == NR {
        f1[++fc1] = $1
        next
}
{       fn = sprintf("out%d.txt", FNR)
        for(i = 1; i <= fc1; i++) printf("%d\n", $1 * f1[i]) > fn
        close(fn)
}' file1.txt file2.txt

If you want to try this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.

Note that this script call awk once (no matter how many lines are in file2.txt) rather than calling awk twice and paste once for each line in file2.txt.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 08-14-2013
Thank you very much for the very clean code and the explanation on my script. My default shell is bash and your code worked perfectly. Smilie

One quick question, what does the following part mean
Code:
f1[++fc1] = $1

bunch of thanks again.
# 6  
Old 08-14-2013
Quote:
Originally Posted by ida1215
Thank you very much for the very clean code and the explanation on my script. My default shell is bash and your code worked perfectly. Smilie

One quick question, what does the following part mean
Code:
f1[++fc1] = $1

bunch of thanks again.
That line creates an array (f1[]) of the values found in the 1st column ($1) of the 1st input file (when FNR {the line number in the of the input lines read from the current file} is equal to NR {the line number of all input lines read from all files}) indexed by the current line number in the file (++fc1). Note that I could have used FNR as the index here, but I need to save the number of lines found in the 1st input file so we can use it in the for loop when we are processing the 2nd input file.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 08-14-2013
Thanks again for the explanation, Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

awk script to check and throw error for multiplication result

I need to multiply column1 and column3 data and need to compare it with column5. Need to check multiplication and Throw error if result is greater or less than column5 values, though difference of +/- 2 will be ok Ex - if column1 has 2.4 and column3 has 3.5, it will be ok if column5 have value... (13 Replies)
Discussion started by: as7951
13 Replies

3. Shell Programming and Scripting

Multiplication of two files and and sorting

Hi, I have 2 ASCII files, say file1 AAAAA 3.465830E-12 BBBBB 4.263280E-08 CCCCC 1.113320E-17 DDDDD 0.000000E+00 ... file2 with as many lines as file1 3.932350E-12 1.194380E-07 4.901480E-17 0.000000E+00 3.921180E-40 (3 Replies)
Discussion started by: f_o_555
3 Replies

4. Shell Programming and Scripting

Awk: Multiple Replace In Column From Two Different Files

Master_1.txt 2372,MTS,AP 919821,Airtel,DL 0819,MTS,MUM 919849788001,Airtel,AP 1430,Aircel MP,20 405899143999999,MTS,KRL USSDLIKE,MTS,DEL Master_2.txt 919136,DL 9664,RAJ 919143,KOL 9888,PUN Input File: (4 Replies)
Discussion started by: siramitsharma
4 Replies

5. Shell Programming and Scripting

Selective multiplication of two columns in two files

Hi again, I have two files e.g. file2 e.g. file1 BB152 6.279650E+02 AA124 6.279650E+02 AA124 6.0273E-01 9.7800E-01 AA124 6.3239E-01 9.7800E-04 AA124 6.4585E-01 7.3839E-02 BB152 6.6250E-01 2.4450E-04 BB152 7.0932E-01 1.3496E-02 CC124 7.1378E-01 2.2690E-02 CC124 7.2279E-01... (8 Replies)
Discussion started by: f_o_555
8 Replies

6. Shell Programming and Scripting

multiplication of two files based on the content of the first column

Hi, This is something that probably it is more difficult to explain than to do. I have two files e.g. FILE1 A15 8.3102E+00 3.2000E-04 A15 8.5688E+00 4.3000E-05 B13 5.1100E-01 1.9960E+00 B16 5.1100E-01 2.3000E-03 B16 8.6770E-01 1.0000E-07 B16 9.8693E-01 3.4000E-05... (4 Replies)
Discussion started by: f_o_555
4 Replies

7. UNIX for Dummies Questions & Answers

Matrix multiplication with different files

Hi, i have file1 which looks like: x1 y1 z1 x2 y2 z2 ...(and so on) and file2 which looks like: a11 a12 a13 a21 a22 a23 a31 a32 a33 and i want to replace file1 with the following values: x1' y1' z1' x2' y2' z2' ...(and so on) (2 Replies)
Discussion started by: ezitoc
2 Replies

8. Shell Programming and Scripting

using awk multiplication

Suppose i have a file A 1*2*3*4 2*4*4*22 and second file B 2*3*4*5 4*4*6*7 By multiplying file A by file B that is file A by first column in file B respectively output shud be 2*6*12*20 8*16*24*154 my code is =$1 next } {for (f=1;f<=NF;f++) (2 Replies)
Discussion started by: cdfd123
2 Replies

9. Shell Programming and Scripting

How to extract a column from two different files in AWK?

Hi guys, I need help in extracting one column of numbers from two different files and display it in a output file. In specific, I want to extrac the column no.2 ($2) from each file, file1.txt, file2.txt. Then place both extracted columns in a one file, out.txt. the line command I use to... (7 Replies)
Discussion started by: solracq
7 Replies

10. Shell Programming and Scripting

awk compare column between 2 files

Hi, I would like to compare file1 and file2 file1 1 2 3 file2 1 a 2 b 3 c 4 d The result should only print out "d" in file 2. Thanks (3 Replies)
Discussion started by: phamp008
3 Replies
Login or Register to Ask a Question