Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Generate files from a template with values from a list
- X-seq: zsh-users 11470
- From: Phil Pennock <zsh-workers+phil.pennock@xxxxxxxxxxxx>
- To: zzapper <david@xxxxxxxxxx>
- Subject: Re: Generate files from a template with values from a list
- Date: Thu, 3 May 2007 23:03:11 -0700
- Cc: zsh-users@xxxxxxxxxx
- Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=first1; d=spodhuis.org; h=Received:Date:From:To:Cc:Subject:Message-ID:Mail-Followup-To:References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; b=YgFWs/qyG6IEIEbLbFW6v/UGSM0jHcmCG4Ra2kro4zKZ0CrHIpwINPG/yjwrmRAyGmY0Or05xrlTVnFAauN1jnags1jVfZIq5E3lFoK2YnS0OsV3F19b7CalmXN/VdTdgP3nZpEY3xiruU4QPjfgbMPOrvR5wTqGszgKP9SKyiU=;
- In-reply-to: <Xns9926797B3DB1zzappergmailcom@xxxxxxxxxxx>
- Mail-followup-to: zzapper <david@xxxxxxxxxx>, zsh-users@xxxxxxxxxx
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <Xns9926797B3DB1zzappergmailcom@xxxxxxxxxxx>
On 2007-05-03 at 23:45 +0000, zzapper wrote:
> I have an html template file which contains two fields xx_ and yy_
>
> I need to generate a number of copies of pages from this template each time
> replacing the fields xx_ and yy_ with values from a list.
>
> The list contains 49 xx_ yy_ pairs eg
>
> 11,234
> 15,235
> 21,236
> ..
> ..
> etc
>
> So I want to create 49 pages each populated from one pair from the list
You don't say how those new files will be named. I'm sticking 1..49 in
them by using a shell variable "index" below.
If by list you mean a file with those items in it:
local -i index=0
while IFS=, read xx yy
do
index+=1
perl_that_is_below
done < listfile
otherwise, if you mean you have an array in zsh, then:
local -i index=0
for z in $list; do xx=${z%,*}; yy=${z#*,}
index+=1
perl_that_is_below
done
Note that this Perl is not safe if $xx or $yy can contain arbitrary
text, or anything with backslashes; if that is the case, the safest way
to pass them without any escaping issues is probably to pass via the
environment and use something like $xx = quotemeta $ENV{xx}
(and use single-quotes at the shell level to let those dollars through
to Perl).
perl <template >newfile.$index.html -pe "s/xx_/$xx/g; s/yy_/$yy/g"
All untested. E&OE. "man perlrun" for information on "-p".
You could do it all in Perl, but then use lose the convenience of
"perl -p"; you could do it all in zsh, I'm sure, but it's easier and
cleaner to do the substitution stuff in Perl. For me. Other people who
have minds less contaminated by Perl may well disagree.
-Phil
Messages sorted by:
Reverse Date,
Date,
Thread,
Author