Hi,
Hoping someone can help nudge me the right direction for this problem. I've got a script that parses some data from a csv file, and generates output files (1 for each row) based on this data. As written at the moment this works finr. However I'm tring to replace the many print lines with something that pulls in the template for the output from a text file and then parses the csv columns into that to produce the output.
The existing code is a little like this (though many more print lines)
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
my $file = 'sites.csv';
my $csv = Text::CSV->new();
open (CSV, "<", $file) or die $!;
while (<CSV>) {
next if ($. == 1);
if ($csv->parse($_)) {
my @columns = $csv->fields();
open (MYOUTFILE, ">>001--$columns[0]--877-$columns[3]--autoconfig.txt");
print MYOUTFILE "no service pad \n";
print MYOUTFILE "service timestamps debug datetime msec\n";
print MYOUTFILE "service timestamps log datetime msec\n";
print MYOUTFILE "no service password-encryption\n";
print MYOUTFILE "!\n";
print MYOUTFILE "hostname $columns[0]\n";
print MYOUTFILE "!\n";
print MYOUTFILE "boot-start-marker\n";
print MYOUTFILE "boot-end-marker\n";
print MYOUTFILE "!\n";
print MYOUTFILE "logging buffered 51200 warnings\n";
print MYOUTFILE "enable secret $columns[12]\n";
close(MYOUTFILE);
} else {
my $err = $csv->error_input;
print MYOUTFILE "Failed to parse line: $err";
}
}
close CSV;
The text file I'm try to import contains;
" no service pad
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
"hostname $columns[0]"
!
boot-start-marker
boot-end-marker
!
logging buffered 51200 warnings
enable secret $columns[8]"
Every attempt I've made results in outputing the test files with the $columns exactly as imported without replacing them with the data from the csv file. Can anybody point me in the right direction for what I'm trying to achieve?
many thanks.
Hoping someone can help nudge me the right direction for this problem. I've got a script that parses some data from a csv file, and generates output files (1 for each row) based on this data. As written at the moment this works finr. However I'm tring to replace the many print lines with something that pulls in the template for the output from a text file and then parses the csv columns into that to produce the output.
The existing code is a little like this (though many more print lines)
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
my $file = 'sites.csv';
my $csv = Text::CSV->new();
open (CSV, "<", $file) or die $!;
while (<CSV>) {
next if ($. == 1);
if ($csv->parse($_)) {
my @columns = $csv->fields();
open (MYOUTFILE, ">>001--$columns[0]--877-$columns[3]--autoconfig.txt");
print MYOUTFILE "no service pad \n";
print MYOUTFILE "service timestamps debug datetime msec\n";
print MYOUTFILE "service timestamps log datetime msec\n";
print MYOUTFILE "no service password-encryption\n";
print MYOUTFILE "!\n";
print MYOUTFILE "hostname $columns[0]\n";
print MYOUTFILE "!\n";
print MYOUTFILE "boot-start-marker\n";
print MYOUTFILE "boot-end-marker\n";
print MYOUTFILE "!\n";
print MYOUTFILE "logging buffered 51200 warnings\n";
print MYOUTFILE "enable secret $columns[12]\n";
close(MYOUTFILE);
} else {
my $err = $csv->error_input;
print MYOUTFILE "Failed to parse line: $err";
}
}
close CSV;
The text file I'm try to import contains;
" no service pad
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
"hostname $columns[0]"
!
boot-start-marker
boot-end-marker
!
logging buffered 51200 warnings
enable secret $columns[8]"
Every attempt I've made results in outputing the test files with the $columns exactly as imported without replacing them with the data from the csv file. Can anybody point me in the right direction for what I'm trying to achieve?
many thanks.