title: Linux awk merge two files
body: <p>I have below script to combine two files.</p>
  
  <pre><code>awk -F"\t" '
      {key = $1}
      !(key in result) {result[key] = $0; next;}
      { for (i=2; i &lt;= NF; i++) result[key] = result[key] FS $i }
      END {
          PROCINFO["sorted_in"] = "@ind_str_asc"    # if using GNU awk
          for (key in result) print result[key]
      }
  ' $1 $2 &gt; $3 
  </code></pre>
  
  <p>First column is key and both $1 and $2.
  But if $2 column has a key but $1 column does not have key.</p>
  
  <p>then also it's combining except $1 row.</p>
  
  <p>I want to combine only incase $1 key is existed.
  How can I simply combine this two files?</p>
  
  <p>For example,</p>
  
  <p>File 1</p>
  
  <pre><code>Key    Column1  Column2  Column3  
  Test1    500     400     200               
  Test2    499     400     200               
  Test5    600     200     150               
  Test6    600     199     150               
  Test7    599     199     100               
  </code></pre>
  
  <p>File2</p>
  
  <pre><code>Key    Column4   Column5
  Test1    Good     Good                    
  Test2    Good     Good
  Test3    Good     Good                    
  Test4    Good     Good
  Test5    Good     Good                    
  Test6    Good     Good
  Test7    Good     Good
  </code></pre>
  
  <p>Current Combine</p>
  
  <pre><code>Key    Column1  Column2  Column3  Column4   Column5
  Test1    500     400     200       Good     Good     
  Test2    499     400     200       Good     Good      
  Test5    600     200     150       Good     Good          
  Test6    600     199     150       Good     Good          
  Test7    599     199     100       Good     Good  
  Test3    Good    Good  
  Test4    Good    Good  
  </code></pre>
  
  <p>Expected Combine.</p>
  
  <pre><code>Key    Column1  Column2  Column3  Column4   Column5
  Test1    500     400     200       Good     Good     
  Test2    499     400     200       Good     Good      
  Test5    600     200     150       Good     Good          
  Test6    600     199     150       Good     Good          
  Test7    599     199     100       Good     Good 
  </code></pre>
  
  <p>Thank you!</p>