Handling 3 Files using awk

That worked for me:

awk -F, 'FNR==1{++f} \
  f==1 && $2==7 {a1[$1]++; a2[$2]=$3; o=$0} \
  f==2 && a1[$1] {o=o","$3","$4; a3[$3]=$4} \
  f==3 && a3[$1] && $2==a3[$1] && a2[$3] && $4<a2[$3] {print o}' \
file1 file2 file3

Explanation:

  • The first line (FNR==1{++f}) increments the file index to later determine in which file we are 1-3.
  • file1: if $2 equals 7
    • fill an array a1 with $1 as index and a2 with $2 as index and $3 as value
    • write down the o variable (output) with the first 3 fields
  • file2: if $1 of file2 equals $1 of file1 (prevously written in a1)
    • append $3 and $4 to the output variable o.
    • fill an array a3 with $3 as index and $4 as value.
  • file3: if:
    • $1 equals file2s $3 (index of a3)
    • $2 equals file2s $4 (value of a3)
    • $3 equals file1s $2 (index of a2)
    • $4 is lower than file1s $3 (value of a2)
  • then:
    • print the value of o.