Tarefa quase standart para sed
sed '
/^Block/! D
:1
N
$!{
/\n\s*KEY/! b1
}
\%SAM.*D/D.*D/D\|SAM.*X/Y.*Z/Z%! d
' log
produzir
Block f1
PCO Blockf1
tray:school SAM :XP
X/Y DUPL
KEY Z/Z
Block f2
PCO Blockf2
tray:school SAM :XP
D/D DUPL
KEY D/D
Block f3
PCO Blockf3
tray:school SAM :AP
X/Y DUPL
KEY Z/Z