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; }
In reply to Re: Can I copy a file and rename the file to many new files?
by Roger
in thread Can I copy a file and rename the file to many new files?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |