Perl 读取文本文件
Perl Reading Text File
我正在尝试读入一个文本文件并对文件进行一些拆分,目的是将数据添加到数据库中。
文本文件看起来像。
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...
到目前为止,我已经能够打开文件读取它并进行拆分,但它根本无法扩展。理想情况下,我想做的是将对象组和下一个对象组之间的任何内容放入一个数组中,并将该对象组的各种值作为数组的一部分。
您可以使用以下数据结构。它结构合理且易于访问。
#!/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 = ;
}
else{
if ($_ =~ /description (.*)/){
$data_for_object{$key}{description} = ;
}
if ($_ =~ /network-object host (.*)/){
if (exists $data_for_object{$key}{host}){
push @{$data_for_object{$key}{host}}, ;
}
else{
$data_for_object{$key}{host} = [];
}
}
}
}
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'
}
};
另见:
我正在尝试读入一个文本文件并对文件进行一些拆分,目的是将数据添加到数据库中。 文本文件看起来像。
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...
到目前为止,我已经能够打开文件读取它并进行拆分,但它根本无法扩展。理想情况下,我想做的是将对象组和下一个对象组之间的任何内容放入一个数组中,并将该对象组的各种值作为数组的一部分。
您可以使用以下数据结构。它结构合理且易于访问。
#!/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 = ;
}
else{
if ($_ =~ /description (.*)/){
$data_for_object{$key}{description} = ;
}
if ($_ =~ /network-object host (.*)/){
if (exists $data_for_object{$key}{host}){
push @{$data_for_object{$key}{host}}, ;
}
else{
$data_for_object{$key}{host} = [];
}
}
}
}
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'
}
};
另见: