in reply to append middle of File
By using a while loop both on the original file and the file to be inserted you never have more than a few lines in memory, so it will work on any size of file.use strict; use warnings; open my $FILEC, '>', 'fileC'; open my $FILEB, '<', 'fileB'; open my $FILEA, '<', 'fileA'; while (my $line = <$FILEB>) { do {local $_; print $FILEC $_ while (<$FILEA>)} if $line =~ m/INSE +RT BEFORE HERE/; print $FILEC $line; }
Of course you should add some tests to see whether opening of the files and writing to them succeeds.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
|
|---|