title: Appending fields in a file to another one based on ID
body: <p>Basically, I need a script that can solve the problem in very short time. I have two files:</p>
  
  <p>$ head -n 6 fcu.tsv</p>
  
  <pre><code>NM576455     0.324009324     0.578896174     2577
  NM539570     0.204545455     0.607877092     2247
  NM337132     0.288973384     0.673636364     792
  NM374379     0.308300395     0.42            762
  NM373443     0.263043478     0.547132867     1383
  NM371839     0.298210736     0.492857143     1512
  </code></pre>
  
  <p>$ head -n 6 mart.tsv</p>
  
  <pre><code>NM539570 ILMN_2199362    15      58.52   protein_coding
  NM576455 ILMN_2195138    1       65.74   protein_coding  nucleus cellular_component      SAM_2
  NM576455 ILMN_2195138    1       65.74   protein_coding  protein binding molecular_function      SAM_2
  NM576455 ILMN_1709067    1       65.74   protein_coding  nucleus cellular_component      SAM_2
  NM576455 ILMN_1709067    1       65.74   protein_coding  protein binding molecular_function      SAM_2
  NM576455 ILMN_2195138    1       65.74   protein_coding  nucleus cellular_component      SAM_type1
  </code></pre>
  
  <p>We need to append the 2nd, 3rd, and 4th fields of fcu.tsv to mart.tsv for each NM id in very short time.</p>
  
  <p>$ head out.tsv</p>
  
  <pre><code>NM539570 ILMN_2199362    15      58.52   protein_coding  0.204545455     0.607877092     2247
  NM576455 ILMN_2195138    1       65.74   protein_coding  nucleus cellular_component      SAM_2 0.324009324   0.578896174     2577
      NM576455 ILMN_2195138    1       65.74   protein_coding  protein binding molecular_function      SAM_2 0.324009324   0.578896174     2577
      NM576455 ILMN_1709067    1       65.74   protein_coding  nucleus cellular_component      SAM_2 0.324009324   0.578896174     2577
      NM576455 ILMN_1709067    1       65.74   protein_coding  protein binding molecular_function      SAM_2 0.324009324   0.578896174     2577
      NM576455 ILMN_2195138    1       65.74   protein_coding  nucleus cellular_component      SAM_type1 0.324009324   0.578896174     2577
  </code></pre>
  
  <p>This is what I did in matlab (I prefer that solution fix the bad codes here to make it faster rather than writing a new one)</p>
  
  <pre><code>fr1 = fopen('fcu.tsv', 'r');
  fr2 = fopen('mart.tsv', 'r');
  
  fw = fopen('out.tsv', 'w');
  
  while feof(fr1) == 0
     line = fgetl(fr1);
     scan = textscan(line, '%s%f%f%d');
  
     frewind(fr2);
  
      while feof(fr2) == 0
          line2 = fgetl(fr2);
          scan2 = textscan(line2, '%s%s%s%f%s%s%s%s');
  
              if scan{1}{1} == scan2{1}{1} 
  
                  fprintf(fw, '%s\t%f\t%f\t%d\n', line2, scan{2}, scan{3}, scan{4});
  
              end
  
      end
  
  end
  </code></pre>
  
  <p>Help is appreciated </p>