BEGIN \ { NOTHING = "\x1a" } /^@menu/ \ { if(match(Prev[2],/^@end menu/) && match(Prev[1],/^[ \t]+/)) { $0 = Prev[2] = NOTHING } else for(i=2; i>0; i--) if(match(Prev[i],/^@end menu/)) $0 = Prev[i] = "" } { if(FNR>2 && Prev[2]!=NOTHING) print Prev[2] # Prev[3] = Prev[2] Prev[2] = Prev[1] Prev[1] = $0 } END \ { print Prev[3] print Prev[2] print Prev[1] }