Thanks Kenosis!!
Your help is invaluable..
Ok my .pl as it stands is as follows;
#!/usr/bin/perl
#
use strict;
use warnings;
$ARGV[0] or die "Usage: $0 <filename> [<filename>] ...";
my ( $template_file_name, $templateText, %hash ) = '';
my @vars = split "\n", <<END;
##location##
##rtrname##
##loop0-ip##
##ospf-id##
##ospf-area##
##ospf-network##
##ospf-mask##
##eth00-ip##
##eth00-sm##
END
while (<>) {
chomp;
my @fields = split /,/;
my $templateFN = pop @fields;
if ( $template_file_name ne $templateFN ) {
$template_file_name = $templateFN;
undef $templateText;
}
@hash{@vars} = @fields;
$templateText //= getTemplateText($template_file_name);
my $templateTextCopy = $templateText;
$templateTextCopy =~ s/$_/$hash{$_}/g for keys %hash;
my $ofile_name = './Config_Output/' . $hash{'##rtrname##'} . '.txt
+';
open my $fh, '>', $ofile_name or die "$ofile_name: $!";
print $fh $templateTextCopy;
close $fh;
}
sub getTemplateText {
my ($template_file_name) = @_;
local $/;
open my $fh, '<', './Templates/' . $template_file_name . '.txt' or
+ die "$template_file_name: $!";
$templateText = <$fh>;
close $fh;
return $templateText;
}
I still have a couple of problems, I was hoping you may be able to help with..
1. Is there a way that I can ignore the first line of the CSV? - In the CSV the first line is just column descriptions, and is not needed for the output..
2. I can still not seem to strip the newline off the end of the CSV.. I have tried to replace chomp
with
s/\r\n// but that doesn't seem to do it.. unfortunately the csv needs to be edited with MS Excel, so when saving that, it adds its extra MS stuff to the file
3. Even better - but i don't know if this is even possible.. would there be a way, so that rather than specifying all the template variables (i.e. ##eth00-ip##, ##location## etc), I could have them all listed on the second line of the CSV, and then the code can draw those values out...?
I really appreciate your assistance, and will definitely have to buy you a beer!!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.