title: Linux Combine two files by column
body: <p>I am trying to combine two files as below (Intersection)</p>
  
  <pre><code>ID     Name  Telephone       
  1      John     011
  2      Sam      013
  3      Jena     014
  4      Peter    015
  </code></pre>
  
  <p>Second file Test2.txt</p>
  
  <pre><code>1       Test1    Test2
  2       Test3    Test4
  3       Test5    Test6
  4       Test7    Test8
  5       Test7    Test8
  6       Test7    Test8
  7       Test7    Test8
  8       Test7    Test8
  9       Test7    Test8
  </code></pre>
  
  <p>Then Final result</p>
  
  <pre><code>ID     Name  Telephone    Remark1  Remark2
  1      John    011        Test1    Test2
  2      Sam     013        Test3    Test4
  3      Jena    014        Test5    Test6
  4      Peter   015        Test7    Test8
  </code></pre>
  
  <p>I did like this as below,</p>
  
  <pre><code>awk -F"\t" '
      {key = $1 }
      NR == 1 {header = key}
      !(key in result) {result[key] = $0; next}
      { for (i=2; i &lt;= NF; i++) result[key] = result[key] FS $i }
      END {
          print result[header]
          delete result[header]
          PROCINFO["sorted_in"] = "@ind_str_asc"    
          for (key in result) print result[key]
      }
  ' Test1.txt Test2.txt &gt; result.txt
  </code></pre>
  
  <p>And I just notice that this is Union set.
  Including all data Test1 and Test2. </p>
  
  <p>I would like to show only for Intersection case as what I expected result.
  (1, 2 ,3 ,4) only</p>
  
  <p>Do you guys have any idea? Thanks!</p>