while ( <$fh> ) { print if /X/ .. /^\s*$/ and not /^\s*$/; }