in reply to Re^2: append middle of File
in thread append middle of File
You might want to include a couple of "rename" steps after the outer while loop (rename the old "secondary.file" to "secondary.file.old", rename the output file to "secondary.file").#!/usr/bin/perl use strict; use warnings; ( @ARGV == 3 and -f $ARGV[0] ) or die "Usage: $0 primary.file secondary.file insertion_point_ +pattern\n"; open( my $in1, "<", $ARGV[0] ) or die "$ARGV[0]: $!\n"; open( my $in2, "<", $ARGV[1] ) or die "$ARGV[1]: $!\n"; open( my $out, ">", "$ARGV[1].including.$ARGV[0]" ) or die "$ARGV[1].including.$ARGV[0]: $!\n"; while ( <$in2> ) { print $out $_; # assuming you want content added after target p +attern if ( /$ARGV[2]/ and $in1 ) { while ( <$in1> ) { print $out $_; } close $in1; $in1 = undef; } }
Note that if the given pattern never matches anything in the secondary file, the primary file never gets read or added to the output (output will be identical to input).
|
|---|