my ($output,$match_flag); open (DATA, "/export/home/webadm/scripts/backup_scripts/backup_data/1stnodupe"); while () { if (/^Switch name complete/) { # match this $output .= $_; $match_flag=1; } elsif (/^\s/ && $match_flag) { # if correct, match this as well $output .= $_; } elsif (/^\S/) { # invalid, so don't match, or match following lines # beginning with whitespace $match_flag=0; } } close(DATA); print $output;