Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way.

File1:

pictures.txt 1.1 1.3
dance.txt 1.2 1.4
treehouse.txt 1.3 1.5

File2:

pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244
dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2 ref4948 1.1
treehouse.txt 1.6 ref8573 1.5 ref3284 1.4 ref5838 1.3 ref4738 1.2 ref4573 1.1

What I want is the file and the version number to be printed into a file between the two version numbers in shown in file1. So the output should be:

pictures.txt 1.3 ref5432 1.2 ref4244 1.1
dance.txt 1.4 ref0695 1.3 ref5738 1.2 ref4948
treehouse.txt 1.5 ref3284 1.4 ref5838 1.3 ref4738 

Also, rdcwayx identified a possible bug in the awk for loop which no one seems to have picked up on:

$ awk 'BEGIN {for (i=1.5;i>=1.2;i-=0.1) print i}'
1.5
1.4
1.3

$ awk 'BEGIN {for (i=15;i>=12;i-=1) print i}'
15
14
13
12

In the first example there should be 4 outputs, however only 3, whilst in the 2nd one we get all 4.

As far as the first question is concerned:

awk 'NR == FNR {
  for (i = 1; (i += 2) <= NF;) {
    r[$1, $(i - 1)] = $i 
    v[$1] = v[$1] ? v[$1] FS $(i - 1) : $(i - 1)
    }
  next    
  }
{ 
  n = split(v[$1], t)
  printf "%s ", $1
  for (i = 0; ++i <= n;) {
    if ($2 <= t && t <= $3) 
      printf "%s %s", t, (r[$1, t] (i <= n ? FS : x))
      }
    print (($1, $2) in r) ? x : $2 FS r[$1, $2]
  }' file2  file1

As far as the second one is concerned: the floating arithmetic in computers works like that, it's not a bug (1.2 is stored as an approximation, it could in reality be 1.20001 or even 1.19998)).

1 Like