Op zondag 2 maart 2014 12:50:40 schreef Geert Stappers:
> Hoi,
> 
> Een programma verwerkt een lijst.
> Doet dat goed, de output is minder.
> 
> <output>
> Setup info foo
> Setup info bar
> Processing data 1
> Processing failed
> Processing data 2
> Processing failed
> Processing data 3
> Processing failed
> Processing data 4
> Processing failed
> Processing data 5
> Processing failed
> Processing data 6
> Processing failed
>    <knip/>
> Processing data n
> Processing failed
> Processing data n+1
> Processing failed
> Processing data n+2
> Processing failed
> Processing data n+3
> Processing failed
> Processing data n+4
> Result: asdfadf
> Result: asdasferqwe
> Result: askjkqerqr
> Processing data n+5
> Processing failed
> Processing data n+6
> Processing failed
> Processing data n+7
> Processing failed
> Processing data n+8
> Processing failed
>    <knip/>
> Processing data n+m
> Processing failed
> Processing data n+m+1
> Processing failed
> Processing data n+m+2
> Processing failed
> Final statistics
> </output>
> 
> 
> Wat ik zou willen hebben, is
> <output>
> Setup info foo
> Setup info bar
> Processing data n+4
> Result: asdfadf
> Result: asdasferqwe
> Result: askjkqerqr
> Final statistics
> </output>
> 
> Dus dat "kopregels" met "Processing data recordnummer" niet getoond 
worden
> als de processing is mislukt. De regels met "Processing failed" zijn ook
> niet nodig.
> 
> Met sed en/of awk krijg ik het niet voormekaar.

Met awk zou dat toch moeten lukken, en is redelijk simpel:

commando | awk '
/Processing data/{
  header=$0; 
  header_printed=0;
}
/Result/{
  if(!header_printed) {
    print header; 
    header_printed=1;
  }
  print $0;
}'

Je mag dat eventueel op één lijn zetten, maar zo leest het wat makkelijker.

Eerste code block wordt uitgevoerd op een lijn 'Processing data', en 
bewaart die lijn ("$0") in de variabele 'header'. De variabele 
'header_printed' wordt op 0 gezet ('false').

Het tweede code block wordt uitgevoerd op een lijn 'Result:', en kijkt eerst 
of 'header_printed' op 0 staat. Zo ja, dan wordt de opgeslagen header 
uitgevoerd, en de variabele 'header_printed' op 1 ("true"). Daarna voeren 
we de huidige lijn gewoon uit.

Voor een lijn "Processing failed" is er geen code block, en is er dus ook 
geen uitvoer.

Wat mist nog: "Setup info" en "Final statistics". Dat lijkt een mooie oefening 
voor de lezer ;-)

-- 
This end should point toward the ground if you want to go to space.

If it starts pointing toward space you are having a bad problem and you
will not go to space today.

  -- http://xkcd.com/1133/

Antwoord per e-mail aan