in reply to Can I copy a file and rename the file to many new files?
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 | |
by Roger (Parson) on Sep 28, 2003 at 21:31 UTC | |
by Anonymous Monk on Sep 29, 2003 at 17:36 UTC | |
by Anonymous Monk on Oct 01, 2003 at 14:02 UTC | |
|
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 | |
by Roger (Parson) on Sep 28, 2003 at 23:02 UTC |