use strict; use warnings; # See perlvar for why $/ = "" causes $record to contain your entire record at once. The point is to read your data record-at-a-time instead of line-at-a-time. local $/ = ""; while ( my $record = ) { # See perlre for why (.+) fetches everything on the same line as the text being matched but doesn't continue onto the next line. Remember, $record is a multi-line string. my ( $pc_name ) = $record =~ /interface (.+)/; my ( $switch_name ) = $record =~ /switchport description (.+)/; print "$pc_name: $switch_name\n"; }