Duplicating lines in a file with Perl
I thought this would be insanely easy but I'm stumped. I want to take each line in a file and duplicate it eight times. Here is the code that I'm using:
#!/usr/bin/perl
use warnings;
use strict;
use diagnostics;
my $inputFile = "Providers list.csv";
my $outputFile = "Providers list2.csv";
open my $FHI, '<', $inputFile || die "cannot open input file";
open my $FHO, '>', $outputFile || die "cannot open output file";
while (<$FHI>) {
for ( my $i = 0; $i < 8; $i++) {
print $FHO "$_\r";
}
}
close $inputFile;
close $outputFile;
What is happening is instead of each line getting duplicated eight times it's repeating the entire file contents eight times. In other words, 开发者_Python百科instead of this:
Line1
Line1
Line1
Line1
Line1
Line1
Line1
Line1
I'm getting this:
Line1
Line2
Line3
Line1
Line2
Line3
Etc., eight times. I don't understand why it's not processing each line individually.
TIA!
If the source file also has Mac line endings, try setting the input record separator with $/ = "\r"
at the start of the script. If that is the issue, then you don't need to also print the \r
with the output, as long as you haven't chomp
ed the data.
You're probably reading a Mac file on a non-Mac platform.
In this case, another option is to convert the line endings outside of your script: it has the advantage that the script itself can be executed unaltered on any platform/source file combination, but of course it's slower (compared to Andy's suggestion).
It consists of three steps: first the newlines conversion (supposing you're on *nix):
perl -pe 's/\r/\n/g' macfile.csv > unixfile.csv
then your script (without appending \r
when printing, as already said by Andy), or simply from the command line:
perl -l0ne 'print "$_\n" x 8' unixfile.csv > unixfile8.csv
which, if you're sure that your original file terminates with a newline, can be further reduced to:
perl -ne 'print $_ x 8' unixfile.csv > unixfile8.csv
and finally:
perl -pe 's/\n/\r/g' unixfile8.csv > macfile8.csv
About your script: just remember that use diagnostics
automatically activates warnings
as well, and that the preferred style for Perl variable names is $all_lowercase_with_underscore
.
Also have a look at Perl newlines across different platforms
The x
operator can be used to print something multiple times, so
print $_ x 8;
精彩评论