foreach my $line (@lines) { if ($line =~ m/\[foo\]/) { print $line; # I do not want this line # how do I print the next lines in @lines # until I reach [foo2]? # once I get to [foo2] I want to print out # everything there also } } #### ; ; foo file ; [foo] a=1 b=2 c=3 [foo2] d=4 e=5 f=6