Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Generate files from a template with values from a list

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
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#*,}

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.


Messages sorted by: Reverse Date, Date, Thread, Author