Creating new file and substituting data 
Author Message
 Creating new file and substituting data

Hi perlers,

I have not been using perl for a while, and have not used it a whole lot
anyway.

I have a little requirement, and I would really appriciate it if someone
could show me a clean way to do what I need to do.

I have a template file eg.

mytemplate.txt and it looks like this

some text
%%XXX%%
more text
%%XXX%%
more text etc.

I also have another file that contains names
fred
sue
bill

etc.

I need a script that will read the two files and for each name in the
name file I need to create a <name>.txt file with the %%XXX%% also
replaced with <name> so in the above example I would end up with 3 files:

fred.txt
sue.txt and
bill.txt

and each file would look like this (except with the appropriate name
substituted)

some text
fred
more text
fred
more text etc.

Thanks very much for any help you can provide.

Derek



Wed, 18 Jun 1902 08:00:00 GMT  
 Creating new file and substituting data
Quote:

> Hi perlers,

> I have not been using perl for a while, and have not used it a whole lot
> anyway.

> I have a little requirement, and I would really appriciate it if someone
> could show me a clean way to do what I need to do.

> I have a template file eg.

> mytemplate.txt and it looks like this

> some text
> %%XXX%%
> more text
> %%XXX%%
> more text etc.

> I also have another file that contains names
> fred
> sue
> bill

> etc.

> I need a script that will read the two files and for each name in the
> name file I need to create a <name>.txt file with the %%XXX%% also
> replaced with <name> so in the above example I would end up with 3 files:

> fred.txt
> sue.txt and
> bill.txt

> and each file would look like this (except with the appropriate name
> substituted)

> some text
> fred
> more text
> fred
> more text etc.

> Thanks very much for any help you can provide.

> Derek

Hi Derek,
my first try would lead in thefollowing direction:
1. Read the file with the names:
           open NAMES, "file_with_names.txt";

2. After setting $/ to undef I would slurp in the complete template
file, presumed it isn't too big:
           undef $/;
           open TEMPLATE, "template_file.txt";
           my $text = <TEMPLATE>;
3. Replace the %%XXX%% n a for(each) loop with the name and write it
into a new file

               $text =~ s/%%XXX%%/$_/g;  # Maybe \%\%XXX\%\%
               open OUT, ">$_.txt";
               print OUT $text;
               close OUT;
           }

Just a thought. HTH

Ulrich
--
Ulrich Ackermann
ORGA Kartensysteme GmbH (SY-PEAT-STA)
Tel.:+49.5254.991-925



Wed, 18 Jun 1902 08:00:00 GMT  
 Creating new file and substituting data

Quote:

>my first try would lead in thefollowing direction:
>1. Read the file with the names:
>           open NAMES, "file_with_names.txt";



Quote:
>2. After setting $/ to undef I would slurp in the complete template
>file, presumed it isn't too big:
>           undef $/;
>           open TEMPLATE, "template_file.txt";
>           my $text = <TEMPLATE>;
>3. Replace the %%XXX%% n a for(each) loop with the name and write it
>into a new file

>               $text =~ s/%%XXX%%/$_/g;  # Maybe \%\%XXX\%\%

That only works the first time through the loop, since after
that $text no longer contains '%%XXX%%'.  You need to make a
copy of $text.

It's not necessary to backslash % -- it's not special in regular
expressions, and hashes (as opposed to hash elements) don't
interpolate.

Quote:
>               open OUT, ">$_.txt";
>               print OUT $text;
>               close OUT;
>           }

--

Washington, DC


Mon, 10 Feb 2003 11:13:25 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. how to substitute strings in a newly created file

2. COMPARING FILES AND CREATE A NEW FILE

3. split and substitute, substitute, substitute

4. Current working dir and create a new file on fly for writing

5. Creating new files and directories

6. Create a new file

7. exclusively create a new file over nfs?

8. Problems with files that a script creates, new Perl user

9. Trouble creating a new file

10. Perl Script to create a new file with Read Write access

11. Creating a new file each day??

12. Finding new files (create after a time stamp)

 

 
Powered by phpBB® Forum Software