#... my @lines = (); open my $inFile, '<', 'svrrp.txt' or die "Couldn't open svrrp.txt: $!"; my $currIface = ""; while (<$inFile>) { chomp; if (my $iface = ($_ =~ /^Interface\s(\w+)$/) { processlines($currIface, @lines); $currIface = $iface; @lines = (); } push @lines, $_; } processlines($currIface, $lines); # to process last interface block #... sub processlines() { my $iface = shift; return unless $iface; # declare any vars foreach (@_) { # do your pattern matching here } # print your findings }