use strict; while( ) { if( /^1$/ ) { my $n = 0; ++$n while defined( $_ = ) and /^\n$/; $n = 1 if /_____ 2/; print '1', "\n" x $n; } print; } __DATA__ 1 b 1 _____ 2