Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import file replace variables and export

Status
Not open for further replies.

hazeyb

ISP
Apr 11, 2008
3
GB
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.
 
There are a number of templating modules on CPAN which will do this for you. The most comprehensive is Template::Toolkit, but for what you need to do one of the simpler ones would probably work just as well.


Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
If I understand.....

input from a file is treated the same as a single-quoted string, in other words, no variable interpolation. If you have what looks like a variable in a text file, its not, its just text. As Steve has said, there are templating systems you can use. You can also review this FAQ on how to expand variables in single-quoted strings:


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top