in reply to How to reorder a text file

I wanted to do two things - read a section at a time, and write the output file from back to front. This allows a single pass through the input file.
Since the sections don't have an obvious ending, reading into the next section is required, but that's easy to fix when it happens :)

#!/usr/bin/perl # https://perlmonks.org/?node_id=1216436 use strict; use warnings; my $inputfile = 'd.1216436.in'; my $outputfile = 'd.1216436.out'; my $pointer = -s $inputfile; # output will be same size as i +nput open my $in, '<', $inputfile or die "$! opening $inputfile"; open my $out, '>', $outputfile or die "$! opening $outputfile"; local $/ = "\nTitle"; # too far, but we will fix it while( <$in> ) # read whole section { s/\n\KTitle\z// and seek $in, -5, 1; # if read too far, fix & backup seek $out, $pointer -= length, 0; # backup output pointer print $out $_; # put in proper place } close $out;

Now if I can only stop giggling :)