Yes, but it's likely you're not going to like it...
Install Text::CSV, and use the following script:
#!/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->new( { binary => 1, eol => $/ } )
or die "Cannot use CSV: " . Text::CSV->error_diag();
for my $csvFile (@ARGV) {
print "\nProcesing file $csvFile ...\n";
open my $csvfh, '<:crlf', $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) ) {
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;
}
If you try it on one machine and it resolves the uninitialized error issue, then there's more diagnostic information to work with.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|