Chris Kilian Chris Kilian - 21 days ago 10
Perl Question

Perl Reading Text File

I am trying to read in a text file and do some splitting up of the file with the intention of adding the data into a database.
The text file looks something like.

object-group network og-net-network-hostname-blah
description blah
network-object host 192.168.0.1
network-object host 192.168.0.2
network-object host 192.168.0.3
network-object host 192.168.0.4
network-object host 192.168.0.5
network-object host 192.168.0.6
object-group network og-net-network-hostname-anotherblah
description blah2
network-object host 192.168.1.1
network-object host 192.168.1.2
network-object host 192.168.1.3
network-object host 192.168.1.4
network-object host 192.168.1.5
network-object host 192.168.1.6


What I am wanting to add to the database is as follows.

Column Name => og-net-network-hostname-blah,og-net-network-hostname-anotherblah
Column Description => blah,blah2
`Column Objects =>192.168.0.1...,192.168.1.1...
`Column Objects =>192.168.0.1...,192.168.1.1...


So far I have been able to open the file read it and do a split but its not scalable at all. What I would ideally like to do is to have anything between object-group and the next object-group put into an array with the various values of that object-group as part of the array.

Answer

You may use below data structure. It's well formed and easy to access.

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

my $filename = 'file.txt';
open (my $fh, "<", $filename) or die $!;

my %data_for_object;
my $key;

while(<$fh>){
    chomp;
    if ($_ =~ /object-group network (.*)/){
        $key = $1;
    }
    else{
        if ($_ =~ /description (.*)/){
            $data_for_object{$key}{description} = $1;
        }
        if ($_ =~ /network-object host (.*)/){
            if (exists $data_for_object{$key}{host}){
                push @{$data_for_object{$key}{host}}, $1;
            }
            else{
                $data_for_object{$key}{host} = [$1];
            }
        }
    }
}

print Dumper \%data_for_object;

Output:

$VAR1 = {
          'og-net-network-hostname-blah ' => {
                                               'host' => [
                                                           '192.168.0.1',
                                                           '192.168.0.2',
                                                           '192.168.0.3',
                                                           '192.168.0.4',
                                                           '192.168.0.5',
                                                           '192.168.0.6'
                                                         ],
                                               'description' => 'blah'
                                             },
          'og-net-network-hostname-anotherblah ' => {
                                                      'host' => [
                                                                  '192.168.1.1',
                                                                  '192.168.1.2',
                                                                  '192.168.1.3',
                                                                  '192.168.1.4',
                                                                  '192.168.1.5',
                                                                  '192.168.1.6'
                                                                ],
                                                      'description' => 'blah2'
                                                    }
        };

Also see: