将 perl 中的散列解析为 CSV 格式
Parsing hash in perl to CSV format
我需要将我的文本文件(我的输出)解析为 csv 格式,其中仅包含来自设备的名称和 Ips。像这样的东西:
Name, ip
LAB-W, 10.66.1.12
LAB-D, 10.66.1.13
我没有使用 perl 进行解析的经验,也许有人可以帮我解决这个问题。
use Net::Cisco::ISE;
use Data::Dumper;
use Text::CSV;
my $ise = Net::Cisco::ISE->new(hostname=>'hostname', username=>'user', password=>'user');
for my $name (keys %{$networkdevices}){
my $device = $ise->networkdevices("id" => $networkdevices->{$name}->id);
print Dumper $device->name;
print Dumper $device->NetworkDeviceIPList;
}
我有这个输出:
$VAR1 = 'LAB-W';
$VAR1 = {
'NetworkDeviceIP' => {
'mask' => '32',
'ipaddress' => '10.66.1.12'
}
};
$VAR1 = 'LAB-D';
$VAR1 = {
'NetworkDeviceIP' => {
'mask' => '24',
'ipaddress' => '10.66.1.13'
}
};
我试过这个脚本(来自this post),但我没有看到输出,也没有错误:
use DBI;
use strict;
use Data::Dumper;
use File::Slurp;
open (FILE, "<./my_output") or die "could not open file: $!";
undef $/;
my $contents = <FILE>;
close(FILE);
my $hash_of_arrays = eval $contents;
for my $key (keys %$hash_of_arrays) {
my $hash = $hash_of_arrays->{$key};
for my $key2 (keys %$hash) {
my $array = $hash->{$key2};
for my $i (0..$#$array) {
print "$i: $$array[$i]\n";
}
}
}
由于数据只是散列,您可以直接在循环中访问它们:
for my $name (keys %{$networkdevices}){
my $device = $ise->networkdevices("id" => $networkdevices->{$name}->id);
print join(',',
$device->name
$device->NetworkDeviceIPList->{NetworkDeviceIP}->{ipaddress}
) . "\n";
}
我需要将我的文本文件(我的输出)解析为 csv 格式,其中仅包含来自设备的名称和 Ips。像这样的东西:
Name, ip
LAB-W, 10.66.1.12
LAB-D, 10.66.1.13
我没有使用 perl 进行解析的经验,也许有人可以帮我解决这个问题。
use Net::Cisco::ISE;
use Data::Dumper;
use Text::CSV;
my $ise = Net::Cisco::ISE->new(hostname=>'hostname', username=>'user', password=>'user');
for my $name (keys %{$networkdevices}){
my $device = $ise->networkdevices("id" => $networkdevices->{$name}->id);
print Dumper $device->name;
print Dumper $device->NetworkDeviceIPList;
}
我有这个输出:
$VAR1 = 'LAB-W';
$VAR1 = {
'NetworkDeviceIP' => {
'mask' => '32',
'ipaddress' => '10.66.1.12'
}
};
$VAR1 = 'LAB-D';
$VAR1 = {
'NetworkDeviceIP' => {
'mask' => '24',
'ipaddress' => '10.66.1.13'
}
};
我试过这个脚本(来自this post),但我没有看到输出,也没有错误:
use DBI;
use strict;
use Data::Dumper;
use File::Slurp;
open (FILE, "<./my_output") or die "could not open file: $!";
undef $/;
my $contents = <FILE>;
close(FILE);
my $hash_of_arrays = eval $contents;
for my $key (keys %$hash_of_arrays) {
my $hash = $hash_of_arrays->{$key};
for my $key2 (keys %$hash) {
my $array = $hash->{$key2};
for my $i (0..$#$array) {
print "$i: $$array[$i]\n";
}
}
}
由于数据只是散列,您可以直接在循环中访问它们:
for my $name (keys %{$networkdevices}){
my $device = $ise->networkdevices("id" => $networkdevices->{$name}->id);
print join(',',
$device->name
$device->NetworkDeviceIPList->{NetworkDeviceIP}->{ipaddress}
) . "\n";
}