Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello
I have a text file "MAINFILE.TXT" with some text in the file like this.
"MAINFILE.TXT"
someinfo
HOST
IP
someinfo

I want to copy this file and give it a new name for every file (alot of files).
The files should be
MAINFILE_host1.TXT
MAINFILE_host2.TXT
...

The names host1, hosts2 and so on are in a text file I want to read from like this.
"HOSTS.TXT"
host1
host2
...
Can I read in the "HOSTS.TXT" in an array and use the rename on the array?.
Or is there another way to do it?.

If it can be done can I also replace text in these new files from the "HOSTS.TXT" if I also have the IP adress for the hosts?.

"HOSTS.TXT"
host1, 192.168.1.1
host2, 192.168.1.2

I want files like this
MAINFILE_host1.TXT
someinfo
host1
192.168.1.1
someinfo

and so on.

The "HOSTS.TXT" is going to change and I want to generate these files again.


//Anders Andersson
  • Comment on Can I copy a file and rename the file to many new files?

Replies are listed 'Best First'.
Re: Can I copy a file and rename the file to many new files?
by Roger (Parson) on Sep 28, 2003 at 09:39 UTC
    You can try the following code I quickly wrote. It's just a quick and simple hack. If you want to have command-line options, then you need to use the Getopt::Long module from CPAN.
    use IO::File; use strict; # Load host list my $host = new IO::File "host.txt", "r" or die "Can not open host list file!"; my @hosts = <$host>; undef $host; foreach (@hosts) { chomp; s/\s+//g; # get rid of spaces } # Load input lines my $input = new IO::File "mainfile.txt", "r" or die "Can not open file!"; my @lines = <$input>; undef $input; foreach (@lines) { chomp }; # Create output files for hosts my $prefix = "mainfile_"; foreach (@hosts) { my ($hostname, $ipaddr) = split/,/; # Create the output file, and replace the # HOST and IP lines with actual data my $output = new IO::File "${prefix}${hostname}.txt", "w" or die "Can not create file!"; foreach my $txt (@lines) { if ($txt eq "HOST") { print $output "$hostname\n"; } elsif ($txt eq "IP") { print $output "$ipaddr\n"; } else { print $output "$txt\n"; } } # explicitly close the output file # not required, but good for the eye undef $output; }
      Thanky very much for the quick answer.
      The code works great!.
      I have only one more question.
      If the "MAINFILE.TXT" also contain the HOST and IP on several lines like this.
      "MAINFILE.TXT"
      someinfo
      HOST
      IP
      someinfo
      This is my computer HOST and it has the adress IP.
      someinfo someinfoHOSTsomeinfo
      How can I also replace these?.

      Yes It it should be one line
      someinfo someinfoHOSTsomeinfo


      //Anders Andersson
        If you want to replace all occurances of HOST and IP in your mainfile.txt during the copy process, you just need to replace the printing part:

        ... foreach my $txt (@lines) { # replace HOST and IP with $hostname and $ipaddr $txt =~ s/HOST/$hostname/g; $txt =~ s/IP/$ipaddr/g; print $output "$txt\n"; }
        This will replace ALL occurances of HOST and IP in your mainfile.txt.
      Isn't Getopt::Long part of the core distribution? I don't remember installing it on my machine and it's there. Then again, it could just be that my brain needs to be upgraded.
        You are probably right. I just mentioned CPAN because in case a certain module is not found, you know where to get it. :-)
Re: Can I copy a file and rename the file to many new files?
by jonnyfolk (Vicar) on Sep 28, 2003 at 10:33 UTC

    Warning:The following script is based on your first definition of HOSTS.TXT. You would have to use a split or other means to extract the host name if your HOSTS.TXT is actually as in your second definition.

    In fact the second part is rather unclear - perhaps you could provide an exact explanation of what is in your files and what you wish to achieve?

    #!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI ':standard'; open MAINFILE, "../test/MAINFILE.TXT" or die $!; my @all = <MAINFILE>; close MAINFILE; open HOST, "../test/HOSTS.TXT" or die $!; foreach my $line (<HOST>) { chomp $line; open HOSTFILE, "> ../test/results/MAINFILE_$line.txt" or die $!; print HOSTFILE "@all"; close HOSTFILE; } close HOST; print "Content-type: text/html\n\n"; print "done";