zsh-users
 help / color / mirror / code / Atom feed
* Generate files from a template with values from a list
@ 2007-05-03 23:45 zzapper
  2007-05-04  6:03 ` Phil Pennock
  0 siblings, 1 reply; 4+ messages in thread
From: zzapper @ 2007-05-03 23:45 UTC (permalink / raw)
  To: zsh-users


Hi
Don't know where to start with this shell or Perl.

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

ideas?

-- 
zzapper
http://successtheory.com/tips/ Vim, Zsh, MySQL Tips


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Generate files from a template with values from a list
  2007-05-03 23:45 Generate files from a template with values from a list zzapper
@ 2007-05-04  6:03 ` Phil Pennock
  2007-05-04 10:02   ` zzapper
  0 siblings, 1 reply; 4+ messages in thread
From: Phil Pennock @ 2007-05-04  6:03 UTC (permalink / raw)
  To: zzapper; +Cc: zsh-users

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Generate files from a template with values from a list
  2007-05-04  6:03 ` Phil Pennock
@ 2007-05-04 10:02   ` zzapper
  2007-05-04 15:26     ` Lorenzo E. Danielsson
  0 siblings, 1 reply; 4+ messages in thread
From: zzapper @ 2007-05-04 10:02 UTC (permalink / raw)
  To: zsh-users

Phil Pennock <zsh-workers+phil.pennock@spodhuis.org> wrote in
news:20070504060311.GA7884@redoubt.spodhuis.org: 

> 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
Thanks Phil,
In fact a pure Perl solution turned out to be trivial.
1) Put list of xx_, yy_ pairs into an array of arrays
2) Put template into a hereis variable
3) for loop,substitute,write to file, Bob's Your Aunty

always been interested in where the shell/perl script boundary is.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Generate files from a template with values from a list
  2007-05-04 10:02   ` zzapper
@ 2007-05-04 15:26     ` Lorenzo E. Danielsson
  0 siblings, 0 replies; 4+ messages in thread
From: Lorenzo E. Danielsson @ 2007-05-04 15:26 UTC (permalink / raw)
  To: zsh-users

On Fri, May 04, 2007 at 10:02:11AM +0000, zzapper wrote:
> Phil Pennock <zsh-workers+phil.pennock@spodhuis.org> wrote in
> news:20070504060311.GA7884@redoubt.spodhuis.org: 
> 
> > 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
> Thanks Phil,
> In fact a pure Perl solution turned out to be trivial.
> 1) Put list of xx_, yy_ pairs into an array of arrays
> 2) Put template into a hereis variable
> 3) for loop,substitute,write to file, Bob's Your Aunty
> 
> always been interested in where the shell/perl script boundary is.
> 
> 
I really don't think there is a clear boundary. It's a matter of which
gets the job done (for you). In this case you found that implementing
the script in Perl did the job. Somebody else might have (equally
trivially) done the same thing in zsh, Ruby, or maybe even in Monty 
Python's Flying Circus.

There are a few cases where I would argue that Perl is definately a
better choice than zsh. Equally, there are a few cases where I would say
that zsh is definately the better tool for the job. In all other cases
(that huge grey zone in between), it all depends on which one you are 
more familiar with, what mood you are in, what you had for breakfast 
and so on.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-05-04 15:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-03 23:45 Generate files from a template with values from a list zzapper
2007-05-04  6:03 ` Phil Pennock
2007-05-04 10:02   ` zzapper
2007-05-04 15:26     ` Lorenzo E. Danielsson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).