Perl阅读文本文件

时间:2016-11-15 08:16:45

标签: perl perl-data-structures

我正在尝试读取文本文件并对文件进行一些拆分,以便将数据添加到数据库中。 文本文件看起来像。

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

我想要添加到数据库的内容如下:

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...

到目前为止,我已经能够打开文件读取它并进行拆分,但它根本不可扩展。我理想的做法是将object-group和下一个object-group之间的任何东西放入一个数组中,该对象组的各种值作为数组的一部分。

1 个答案:

答案 0 :(得分:2)

您可以使用以下数据结构。它结构良好,易于访问。

#!/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;

输出:

$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'
                                                    }
        };

另见: