Help with reading two input files in awk

Hello,

I'm trying to write an awk program that reads two files inputs.
example,

file 1:

0.00017835
0.000176738
0.00018811
0.000189504
0.000188155
0.000180065
0.000178991
0.000178252
0.000182513

file 2:

 1.7871769E-05  1.5139576E-16  1.5140196E-16  1.5139874E-16
 1.7827407E-04  1.6709859E-18  1.6950919E-18  1.6897323E-18
 1.7830577E-04  1.6709414E-18  1.6843205E-18  1.6807697E-18
 1.7833745E-04  1.6709859E-18  1.6843205E-18  1.6807697E-18
 1.7836914E-04  1.6709859E-18  1.6843205E-18  1.6807697E-18
 1.7840082E-04  1.6717005E-18  1.6800867E-18  1.6807697E-18
 1.7843251E-04  1.6717005E-18  1.6800867E-18  1.6807697E-18
 1.7846419E-04  1.6717005E-18  1.6800867E-18  1.6897323E-18
 1.7849589E-04  1.6826009E-18  1.6800867E-18  1.6897323E-18
 1.7852758E-04  1.6891735E-18  1.6843205E-18  1.6897323E-18
 1.7855926E-04  1.6943236E-18  1.6800867E-18  1.6897323E-18
 1.7881277E-04  1.6891735E-18  1.6609087E-18  1.6604721E-18
 1.7884445E-04  1.6891735E-18  1.6565899E-18  1.6604721E-18
 1.7887614E-04  1.6826009E-18  1.6494530E-18  1.6547480E-18
 1.7890782E-04  1.6891735E-18  1.6494530E-18  1.6547480E-18
 1.7893952E-04  1.6943236E-18  1.6494530E-18  1.6547480E-18
 1.7897120E-04  1.6891735E-18  1.6462933E-18  1.6532895E-18
 1.7900289E-04  1.6891735E-18  1.6462933E-18  1.6532895E-18
 1.7903457E-04  1.6826009E-18  1.6462933E-18  1.6532895E-18
 1.7906626E-04  1.6717005E-18  1.6394092E-18  1.6475811E-18
 1.7909794E-04  1.6826009E-18  1.6462933E-18  1.6475811E-18....

where file 2 is much bigger than the file1 and the first column is in descending order.
The problem is that I want every value in file1 to go to the list in file2 and stop until the value in file1 is greater than or equal to that of file2's first column and output the whole row to a new file. Taking in consideration that file2 will contain more data ranging for all values that will be in file1

this is what I have so far but is not working:confused:

awk 'NR>=FNR{value = $1} {if(value>$1){print $1};next}' file1 file2 > file3

so from above sample, what's the expect output?

expected output for the example above is

1.7836914E-04 1.6709859E-18 1.6843205E-18 1.6807697E-18
1.7900289E-04 1.6891735E-18 1.6462933E-18 1.6532895E-18

for only the 1st and 7th entry in file1.

Something like this? If it is fine, remove the echo line.

while read line
do   
    echo $line; 
    awk -v val=$line '{if (val>=$1) {print;exit}}' file2
done < file1
1 Like

It works
The only change I made to your code was the ">" to "<" to look like this (val<=$1)

but the code prints the values of file1 as well as a new row

example:

0.000189449
  1.8945978E-04  1.4725076E-18  1.4416145E-18  1.4723191E-18
0.00017835
  1.7836914E-04  1.6709859E-18  1.6843205E-18  1.6807697E-18
0.000176738
  1.7675308E-04  1.6943236E-18  1.7221128E-18  1.7171747E-18
0.00018811
  1.8812889E-04  1.5341278E-18  1.4887214E-18  1.5153740E-18
0.000189504
  1.8952315E-04  1.4725076E-18  1.4402210E-18  1.4723191E-18
0.000188155
  1.8816059E-04  1.5400582E-18  1.4971780E-18  1.5158510E-18
0.000180065
  1.8008027E-04  1.6634776E-18  1.6213719E-18  1.5959426E-18
0.000178991
  1.7900289E-04  1.6891735E-18  1.6462933E-18  1.6532895E-18
0.000178252
  1.7827407E-04  1.6709859E-18  1.6950919E-18  1.6897323E-18
0.000182513
  1.8252021E-04  1.6103587E-18  1.5602976E-18  1.5690959E-18

which I don't want. In {print;exit} this is the part I don't understand on how to make it only print the data from file 2.

thanks

---------- Post updated at 07:51 PM ---------- Previous update was at 07:43 PM ----------

Never mind. removing the echo works like you say previously.

Final code:

while read line
do   
    awk -v val=$line '{if (val<=$1) {print;exit}}' file2
done < file1

Thanks

With echo $line , It will be easy for verification. So if confirm the result is right, you can remove it.

By the way, don't forget to use code tage in this forum

1 Like