Perl3r has asked for the wisdom of the Perl Monks concerning the following question:
Hi All,
Ok so i have the following script which I have been working on, with the help of some fantastic people on this forum.. I have come to a bit of a wall though, and was hoping that some more eyes might be able to assist.
The background is that I have a CSV file, that contains a collection of IP addresses, and settings that I want to export to a router configuration template..
The first line of the CSV is column descriptions, the second line is a list of variables that is used in the template file - i.e. ##rtrname## in the template file, is replaced with the value in the CSV file.. The last column in the CSV is a list of which template to use - ie router.txt
The problem however is that the CSV file needs to be edited, and maintained within MS Excel. I have successfully got the script running, if the CSV file is maintained in something like TextEditor, but as soon as you edit in Excel, it messes with the format, and adds extra info to the end of each line.. I started to use Text::CSV to try and overcome this, but I have encountered some problems with that
My code as it stands at the moment is:
#!/usr/bin/perl # use strict; use warnings; use Text::CSV; $ARGV[0] or die "Usage: $0 <filename> [<filename>] ..."; my ( $template_file_name, $templateText, %hash ) = ''; my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 2, }) or die "Cannot use CSV: " . Text::CSV->error_diag(); for my $csvFile (@ARGV) { print "\nProcesing file $csvFile ...\n"; open my $csvfh, '<', $csvFile or die "Unable to open $csvFile: $!" +; # Ignore column names in first line my $columnNames = $csv->getline($csvfh); # Get vars from second line my @vars = @{ $csv->getline($csvfh) }; # Process each csv line while ( my $row = $csv->getline($csvfh) ) { next unless @$row; my $templateFN = pop @$row; if ( $template_file_name ne $templateFN ) { $template_file_name = $templateFN; undef $templateText; } # keys: fields from second line; values: fields from csv line @hash{@vars} = @$row; $templateText //= getTemplateText($template_file_name); my $templateTextCopy = $templateText; $templateTextCopy =~ s/$_/$hash{$_}/g for keys %hash; my $ofile_name = $hash{'##rtrname##'} . '.txt'; print "Writing to file: $ofile_name\n"; open my $fh, '>', $ofile_name or die "$ofile_name: $!"; print $fh $templateTextCopy; close $fh; } close $csvfh; } print "\nDone!\n"; sub getTemplateText { my ($template_file_name) = @_; local $/; open my $fh, '<', $template_file_name or die "$template_file_name: + $!"; $templateText = <$fh>; close $fh; return $templateText; }
The problem that i get when I run this code, is the following error:
Can't use an undefined value as an ARRAY reference at test.pl line 22, <$csvfh> line 1.an example of the CSV is as follows
Location,Router Name,Loopback IP,OSPF Process ID,OSPF Area,Network,Mas +k,GIG0/0 IP Address,0/0 Subnet Mask,Template ##location##,##rtrname##,##loop0-ip##,##ospf-id##,##ospf-area##,##ospf +-network##,##ospf-mask##,##eth00-ip##,##eth00-sm##, Sydney,SYD-RTR-001,192.168.15.1,99,0,172.25.16.0,0.0.0.255,172.25.16.6 +,255.255.255.0,router
Any assistance would be greatly appreciated!!
Thanks in advance
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Export CSV v2
by GrandFather (Saint) on Oct 09, 2012 at 00:10 UTC | |
by Perl3r (Novice) on Oct 09, 2012 at 00:15 UTC | |
by GrandFather (Saint) on Oct 09, 2012 at 00:23 UTC | |
by Tux (Canon) on Oct 09, 2012 at 07:00 UTC | |
by Perl3r (Novice) on Oct 09, 2012 at 00:41 UTC | |
Re: Export CSV v2
by choroba (Cardinal) on Oct 09, 2012 at 00:52 UTC | |
by Tux (Canon) on Oct 09, 2012 at 07:09 UTC | |
by Perl3r (Novice) on Oct 09, 2012 at 01:04 UTC | |
by Anonymous Monk on Oct 09, 2012 at 02:00 UTC | |
by Perl3r (Novice) on Oct 09, 2012 at 02:05 UTC | |
by choroba (Cardinal) on Oct 09, 2012 at 07:22 UTC |