REGEX problem

Hi there,
How can we use regex in perl to store the Route Distinguisher (the bold field) and also the underlined and bold lines in the below file?

Note:
These highlighted pattern is redundant through the whole input file. Basically, we just need to extract these fields at least to store them for later use using RegEx in perl and each Route Disnguisher has to go in a hash as the key with its corresponding underlined network patterns as values? The header should be skipped.
thanks:D

Here's our input file:

  Network            Next Hop         From             AS Path
 Route Distinguisher:  577:99
 10.99.99.79/32     67.70.219.79     Local            ?
 Route Distinguisher:  577:10168
 38.44.1.0/30       67.70.219.79     Local            ?
 192.85.111.0/24    38.44.1.1        38.44.1.1        65500 ?
 Route Distinguisher:  700:700
 8.8.8.8/32         70.70.70.1       70.70.70.1       65500 ?
 9.9.9.9/32         70.70.70.1       70.70.70.1       65500 ?
 10.10.10.1/32      70.70.70.1       70.70.70.1       65500 ?
 50.0.0.0/8         80.80.80.1       80.80.80.1       65500 ?
 70.70.70.0/30      70.70.70.1       Local            ?
 71.71.71.0/30      70.70.70.1       70.70.70.1       65500 ?
 80.0.0.0/8         80.80.80.1       80.80.80.1       65500 ?
 80.80.80.0/30      67.70.219.79     Local            ?
 81.81.81.1/32      70.70.70.1       70.70.70.1       65500 ?
 90.0.0.0/30        70.70.70.1       70.70.70.1       65500 ?
 90.90.90.1/32      70.70.70.1       70.70.70.1       65500 ?
 91.0.0.0/30        70.70.70.1       70.70.70.1       65500 ?
 91.91.91.1/32      70.70.70.1       70.70.70.1       65500 ?
 130.130.130.1/32   70.70.70.1       70.70.70.1       65500 ?
 167.70.219.245/32  70.70.70.1       70.70.70.1       65500 ?
 172.26.0.0/16      80.80.80.1       80.80.80.1       65500 ?
 172.26.150.0/25    70.70.70.1       70.70.70.1       65500 ?
 172.26.150.192/26  70.70.70.1       70.70.70.1       65500 ?
 192.85.43.0/24     80.80.80.1       80.80.80.1       65500 ?
 192.85.44.0/24     70.70.70.1       70.70.70.1       65500 ?
 192.85.46.0/24     70.70.70.1       70.70.70.1       65500 ?
 192.168.7.0/30     70.70.70.1       70.70.70.1       65500 ?
 192.168.8.0/30     70.70.70.1       70.70.70.1       65500 ?



What I'm doing here is checking to see if a line has 'Route Distinguisher' in it, and if so then storing the key string in a variable ($buf). If not then I'm storing the values into a hash of arrays of arrays.

#! /usr/bin/perl -w

open(my $inpFile, "<", "inp.txt") || die $?;

my $buf;
my %HoAoA;

while(<$inpFile>)
{
	chomp;
	s/(^\W+|\W+$)//g;

        if (m/Network\s+Next Hop\s+From\s+AS Path/) {
                next;
        }
	if(m/.*Route Distinguisher.*/)
	{
		my ($junk, $rd) = split(/:\W+/);
		$buf = $rd;
	}
	else
	{
		my @arr = split(/\s+/);
		push @{ $HoAoA{$buf} }, [@arr];
	}
}

close $inpFile;

I banged this out quickly so there's no validation, error checking, or warranty but it should get you started.

Here's an unwrap which outputs the Route Distinguisher followed by a colon then comma separated values for each row of input:

foreach $key (keys %HoAoA)
{
	foreach $rowArr ($HoAoA{$key})
	{
		foreach $val (@$rowArr)
		{
			print $key . ":" . join(',', @$val) . "\n";
		}
	}
}

Here's a different way to address the same problem:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

open(FILE, "./data.txt" ) || die "Can't open file:$!\n";

my %results;
my $current_route;
my $i = 0;
while(<FILE>) {
        chomp($_);
        if ($_ =~ m/Network\s+Next Hop\s+From\s+AS Path/) {
                next;
        }
        if ($_ =~ m/Route Distinguisher:\s+([0-9]+:[0-9]+)$/) {
                $current_route=$1;
                $i = 0;
                next;
        }
        if ($_ =~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {
                my @line = split(/\s+/, $_);
                $results{$current_route}[$i]{'Network'} = $line[0];
                $results{$current_route}[$i]{'Next Hop'} = $line[1];
                $results{$current_route}[$i]{'From'} = $line[2];
                $results{$current_route}[$i]{'AS Path'} = $line[3];
                $i++;
        }
}

print Dumper(\%results);

I skip the header, and then create a separate hash key for each Route Distinguisher, and create an array of the lines for each, which are hashes keyed by the column name.

Nice, though that does fail in the case where there is a leading space on a record.

Also, I realized I forgot to skip the header (:rolleyes:) - hope you don't mind my lifting from your code.

You're right. I munged the data slightly from the original post, and got rid of leading spaces, but that's easy enough to handle:

if ($_ =~ m/^\s*\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/) {

I don't mind you lifting from it at all. Happy to help.