Perl Help

Associate
Joined
25 Nov 2004
Posts
1,872
Location
Manchester Uk
I'm a complete beginner at programming, so any technical replies may go straight over my head. I put to get a script that took some variables from a csv file and output this to some files. A shortened version is;

#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;

my $file = 'routerdetails.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-orderno--autoconfig.txt");
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[8]\n";
print MYOUTFILE "!\n";
print MYOUTFILE "aaa new-model\n";
print MYOUTFILE "end\n";
close(MYOUTFILE);
} else {
my $err = $csv->error_input;
print MYOUTFILE "Failed to parse line: $err";
}
}
close CSV;


This works fine, but the full version has a lot of print lines for the configuration is it outputting. I've been trying to figure out how I can have a separate text file with the configuration, inport it, swap out the csv file variable and output it as a bunch of files.

My latest attempt (which doesn't work) is;
#!/usr/bin/perl -w
use strict;
use warnings;
use Text::CSV;

my $infile = '877.txt';
my $file = 'routerdetails.csv';
my $csv = Text::CSV->new();

open (CSV, "<", $file) or die $!;

while (<CSV>) {
next if ($. == 1);
if ($csv->parse($_)) {
my @columns = $csv->fields();
open IN, "< $infile" or die "Can't open $infile : $!";
open OUT, ">001-$columns[0]-877-orderno-autoconfig.txt";
print OUT <IN>;
close IN;
close OUT;
} else {
my $err = $csv->error_input;
print OUT "Failed to parse line: $err";
}
}
close CSV;

This does import the file, but doesn't swap out the variables from the csv file. Can anyone point me in the direction of the correct way to do this?
 
Back
Top Bottom