title: Using unix tools(without resorting to R), how to filter the rows based on some statistics of the grouping?
body: <p>I'd like to filter rows of the following table such that it goes from this :</p>
  
  <pre><code>A   1   3   SOME_OTHER_INFO
  A   1   4   SOME_OTHER_INFO2
  A   2   5   SOME_OTHER_INFO3
  B   1   1   SOME_OTHER_INFO4
  B   2   3   SOME_OTHER_INFO4
  B   2   0   SOME_OTHER_INFO4
  </code></pre>
  
  <p>to that:</p>
  
  <pre><code>A   1   3   SOME_OTHER_INFO
  A   2   5   SOME_OTHER_INFO3
  B   1   1   SOME_OTHER_INFO4
  B   2   0   SOME_OTHER_INFO4
  </code></pre>
  
  <p>Filtering criteria is this: </p>
  
  <p>1) based on the first 2 columns, group rows. </p>
  
  <p>2) Then for each group, select the row where the third column is minimum within group.</p>
  
  <p>3) Return.</p>
  
  <p>Now it's easy to do something like this in <code>R</code> using the package such as <code>plyr</code> using commands like this:</p>
  
  <pre><code>ddply(data, .(first_col, second_col), function(x) { 
                  min_idx = which.min(x$third_col); 
                  return(x[min_idx]) 
                  })
  </code></pre>
  
  <p>But I want to know if there is a <strong>efficient &amp; elegant way</strong> to do this using unix tools on a command line. </p>
  
  <p>Lastly,  I almost found the beautiful solution to this using <code>datamash</code>, which is a recently tool released in GNU, but with some glitches.</p>
  
  <pre><code>$ datamash -g 1,2 min 3 -f &lt; file.txt | cut -f1-4
  
  A 1 3 SOME_OTHER_INFO1
  A 2 5 SOME_OTHER_INFO3
  B 1 1 SOME_OTHER_INFO4
  B 2 3 SOME_OTHER_INFO4 # &lt;-- not the correct row I want to grab
  </code></pre>
  
  <p>The problem was when using "-f" flag, it grabs the first item from each group, not the row that min corresponds to. So if you look at the output above "B 2 3 SOME_OTHER_INFO4" was selected rather than "B 2 0 SOME_OTHER_INFO4".</p>