title: properly join two files based on 2 columns in common
body: <p>I have two files I'm trying to join/merge based on columns <code>1</code> and <code>2</code>. They look something like this, with <code>file1</code> (<code>58210</code> lines) being much shorter than <code>file2</code> (<code>815530</code> lines) and I'd like to find the intersection of these two files based on fields <code>1</code> and <code>2</code> as an index:</p>
  
  <p><code>file1</code>:</p>
  
  <pre><code>2L      25753   33158
  2L      28813   33158
  2L      31003   33158
  2L      31077   33161
  2L      31279   33161
  3L      32124   45339
  3L      33256   45339
  ...
  </code></pre>
  
  <p><code>file2</code>:</p>
  
  <pre><code>2L      20242   0.5     0.307692307692308
  2L      22141   0.32258064516129        0.692307692307692
  2L      24439   0.413793103448276       0.625
  2L      24710   0.371428571428571       0.631578947368421
  2L      25753   0.967741935483871       0.869565217391304
  2L      28813   0.181818181818182       0.692307692307692
  2L      31003   0.36    0.666666666666667
  2L      31077   0.611111111111111       0.931034482758621
  2L      31279   0.75    1
  3L      32124   0.558823529411765       0.857142857142857
  3L      33256   0.769230769230769       0.90625
  ...
  </code></pre>
  
  <p>I've been using the following couple of commands but end up with different numbers of lines:</p>
  
  <pre><code>awk 'FNR==NR{a[$1$2]=$3;next} {if($1$2 in a) print}' file1 file2 | wc -l
  awk 'FNR==NR{a[$1$2]=$3;next} {if($1$2 in a) print}' file2 file1 | wc -l
  </code></pre>
  
  <p>I'm not sure why this happens, and I've tried sorting prior to comparison, just in case I have duplicate lines (based on columns <code>1</code> and <code>2</code>) in either of the files, but it doesn't seem to help. (Any insights on why this is so are also appreciated)</p>
  
  <p>How can I just merge the files so that just the lines of <code>file2</code> that have the corresponding columns <code>1</code> and <code>2</code> in <code>file1</code> get printed, with column <code>3</code> of <code>file1</code> added on, to look something like this:</p>
  
  <pre><code>2L      25753   0.967741935483871       0.869565217391304    33158
  2L      28813   0.181818181818182       0.692307692307692    33158
  2L      31003   0.36    0.666666666666667    33158
  2L      31077   0.611111111111111       0.931034482758621    33161
  2L      31279   0.75    1    33161
  3L      32124   0.558823529411765       0.857142857142857    45339
  3L      33256   0.769230769230769       0.90625    45339
  </code></pre>