#!/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 input 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;