Getting input data from 2 tables

Hello All,

I have one table contains:

Table1:

5
10
30
40
60
80
...

Table 2:

10
20
60
80
120
160
..

Now I have divind table2.col 1/table1.col 1 and put the output in third table.

Can you help me to write this in awk as well as in perl....?

Thanks
Krsnadasa

Hi

Hoping you do not mean to say a database table when you say "table":

$ awk 'NR==FNR{a[i++]=$0;next}{print $0/a[j++]}' file1 file2

Guru.

Hu Guru,

Thanks

Can you please explain me the code how it works? You are right these are not db tables.

Thanks
Krsnadasa.

First, we read the contents of File1(NR==FNR) and store in an array(a[i\+\+]=$0). Then, for every entry in the second file, we divide the number by the corresponding array value and print it(print $0/a[j++]).

Guru.

Alternatively:

awk 'getline p<f {print $1/p}' f=file1 file2
1 Like

Hi Scruit,

Thanks,

I am getting correct out put but code is little difficult for me to understand. Can you please explain once?

Thanks
Krsnadasa.

Sure:

awk '                  # For every line in file2
  getline p<f {        # if reading the next line from the file in variable f (containing the name of file1) into variable p is successful
    print $1/p         # then print field 1 of file2 divided by p
  }
' f=file1 file2        # set variable f to file1 and specify file2 as the input file
1 Like

many Thanks to all specially to Scrutinizer for excellent solution.

Thanks
Krsnadasa