Awk multiple variable array: comparison

Foo.txt

20    40    57    50    22
51    66    26    17    15
63    18    80    46    78
99    87    2    14    14
51    47    49    100    58

Bar.txt

20 22
51 15
63 78
99 55
51 58

How to get output using awk

20 22 57 50
51 15 26 17
63 78 80 46
99 55 - -
51 58 49 100

Thanks
~GH

nawk 'FNR==NR {foo[$1,$NF]=$3 OFS $4;next}{idx=$1 SUBSEP $2; print $0 OFS ((idx in foo)? foo[idx] : "-" OFS "-")}' Foo.txt Bar.txt

Another way :

join Foo.txt Bar.txt |
awk '
{
  if ($NF == $(NF-1)) {
     print $1, $NF, $3, $4;
  } else {
     print $1, $NF, "-", "-";
  }
}
'

Jean-Pierre.

without awk

join Foo.txt Bar.txt | while read A B C D E F
do
    echo -n "$A $F "
    [ $E = $F ] && echo "$C $D" || echo  "- -"
done

so many cats - so little time! :wink:

Thank you vgersh, aigles and frans.
Explanation of the code would be very much appreciated.

nawk 'FNR==NR {foo[$1,$NF]=$3 OFS $4;next}{idx=$1 SUBSEP $2; print $0 OFS ((idx in foo)? foo[idx] : "-" OFS "-")}' Foo.txt Bar.txt

~GH