title: sort according to a column value and take first n lines between the same headers in a file
body: <p>I have an input like this;</p>
  <pre><code>header  score1  score2
  item    1.3 100
  item    2.3 170
  item    4.0 35
  header  score1  score2
  item    2.9 45
  item    1.7 55
  header  score1  score2
  item    0.5 60
  header  score1  score2
  header  score1  score2
  item    1.4 75
  item    2.5 120
  item    3.7 200
  header  score1  score2
  </code></pre>
  <p>I want to consider the lines between two lines including 'header' individually. Sort the lines according to the value in the second column in descending order and take the first two lines with the highest values. Also, add the header at the top. It is known that the list starts with &quot;header score1  score2&quot; .</p>
  <p>So desired output is this;</p>
  <pre><code>header  score1  score2
  item    4.0 35
  item    2.3 170
  header  score1  score2
  item    2.9 45
  item    1.7 55
  header  score1  score2
  item    0.5 60
  header  score1  score2
  header  score1  score2
  item    3.7 200
  item    2.5 120
  header  score1  score2
  </code></pre>
  <p>I am a relatively new awk user so my best methodology for now is separating the steps in words and then doing the code research and application stepwise. Building a code block is something I cannot apply for now.</p>
  <p>So first I have to separately consider every interval between the lines starting with &quot;header&quot;</p>
  <p><strong>1.</strong></p>
  <pre><code>awk '/header/ {p=1;print;next}  /^header/ &amp;&amp; p {p=0;print} p' input.txt
  </code></pre>
  <p>This inputs the same file as expected. What I understand from this is when there is 'header it is printing it and continues to print below lines until another 'header'</p>
  <p>the sorting and taking the first 2 I am doing with this code:</p>
  <p><strong>2.</strong></p>
  <pre><code>sort  -k2 -nr | head -2     # this should be without the header
  </code></pre>
  <p>I am guessing that I have to insert the second code inside the first one somehow, so I would appreciate any help about this.
  Thank you</p>