title: How to get the max value in every group of n rows using AWK?
body: <p>Say I have the following 3 columns in text file:  </p>
  
  <pre><code>1 003 3  
  2 006 1  
  3 005 4  
  4 001 2  
  5 006 7  
  6 002 2  
  7 004 3  
  8 001 6  
  9 002 8  
  10 005 2
  </code></pre>
  
  <p>I want to output 3 columns:</p>
  
  <ul>
  <li>last sequence number in every group of 3,</li>
  <li>$2 of the max $3 value in every group,</li>
  <li>and the max $3 in every group.</li>
  </ul>
  
  <p>Starting from after the first one. So from that input, the output would be:</p>
  
  <pre><code>1 003 3  
  4 005 4  
  7 006 7  
  10 002 8
  </code></pre>
  
  <p>What I tried:</p>
  
  <pre><code>awk \
          'BEGIN{
                  cnt=3;
                  max=0;
          };
  
          {
                  if (cnt == 3){
                          cnt++;
                          max_arr[cnt]=$3;
                          for (i in max_arr){
                                  if (max_arr[i] &gt; max)
                                          { max = max_arr[i] }
                          }
  
                          printf "%s %s %s\n", $1,$2,max;
                          cnt=1;
                          delete max_arr;
                          max=0;
                  }
                  else{
                          cnt++;
                          max_arr[cnt]=$3;
                  }
  }' input_file.txt
  </code></pre>
  
  <p>This gives me:</p>
  
  <pre><code>1 003 3  
  4 001 4  
  7 004 7  
  10 005 8
  </code></pre>
  
  <p>Column 1 and 3 are correct, but 2 is wrong.</p>