in reply to Can I copy a file and rename the file to many new files?

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; }

Replies are listed 'Best First'.
Re: Re: Can I copy a file and rename the file to many new files?
by Anonymous Monk on Sep 28, 2003 at 14:27 UTC
    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.
        Hello

        Thank you very much for all your help.


        //Anders Andersson
        Hello

        I must ask you again about this script.
        I was trying this script but every file that are being created have the same info in it.

        These files have been created.
        mainfile_host1.txt
        mainfile_host2.txt

        Both of these file contain this info.
        someinfo
        host1
        192.168.1.1
        someinfo
        This is my computer host1 and it has the adress 192.168.1.1 someinfo someinfohost1someinfo

        I want the the file mainfile_host2.txt contain this info.
        someinfo
        host2
        192.168.1.2
        someinfo
        This is my computer host2 and it has the adress 192.168.1.2 someinfo someinfohost2someinfo

        But it want do that.


        //Anders Andersson
Re: Re: Can I copy a file and rename the file to many new files?
by Nkuvu (Priest) on Sep 28, 2003 at 22:14 UTC
    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. :-)