use strict; use warnings; my @grep = ; my $ip = 'whatever'; if (@grep) { my $hop; my $static; my $tag; my $vpna; my $dns; my %results; print "\nFound some static routes...\n"; while (my $line = shift @grep) { if ($line =~ m/^(\S+):\s+static-route (\S+) next-hop (\S+)( preference \d+|)( bfd-enable|) tag (\d+)/ ) { ($dns, $static, $hop, $tag) = ($1, $2, $3, $6); $line = shift @grep; $line =~ m/^(\S+)-\s+service-name (\S+)/; $vpna = $2; $results{$dns}{$vpna}{$static}{$hop} = $tag; } } #print Dumper \%results; for my $box (keys %results) { print "\n$box\n"; for my $service (keys %{$results{$box}}) { for my $static (keys %{$results{$box}{$service}}) { for my $hop (keys %{$results{$box}{$service}{$static}}) { print "$static\t next-hop $hop\t"; print "tag $results{$box}{$service}{$static}{$hop}, service $service\n"; print "\n"; } } } } } else { print "No static routes exist for $ip\n"; } __DATA__ dnsname1: static-route 1.1.1.1/30 next-hop 2.2.2.2 preference 200 bfd-enable tag 685 dnsname1- service-name "XYZ12345-s12345" dnsname2: static-route 3.3.3.3/30 next-hop 4.4.4.4 bfd-enable tag 635 dnsname2- service-name "XYZ67891-s67890"