I have an input like this;
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
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 "header score1 score2" .
So desired output is this;
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
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.
So first I have to separately consider every interval between the lines starting with "header"
1.
awk '/header/ {p=1;print;next} /^header/ && p {p=0;print} p' input.txt
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'
the sorting and taking the first 2 I am doing with this code:
2.
sort -k2 -nr | head -2 # this should be without the header
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