
Include file1 at a particular point in file2
> I have this problem where I need to include file1 at a
>particular point in file2, for example,
>
>Let file1 be 10 lines long.
>Let file2 be 50 lines long.
>
>I want to include file1 at the 25th line of file2 (say), so that the
>resulting file2 is 60 lines long, how do I go about doing it?
open(FILE1, "<file1") || due "Can't open file1: $!\n";
open(FILE2, "<file2") || die "Can't open file2: $!\n";
open(OUTPUT, ">file2.tmp") || die "Can't open file2.tmp for output: $!\n";
$line = 1;
while (<FILE2>) {
print OUTPUT;
if ( $line++ == 25 ) {
print OUTPUT <FILE1>;
}
}
close(OUTPUT) || die "Couldn't close file2.tmp: $!\n";
rename ("file2.tmp", "file2");
I'd expect you (Barry) to know that you can use $. ($NR,
$INPUT_LINE_NUMBER) instead, so it reads:
while (<FILE2>) {
print OUTPUT;
print OUTPUT <FILE1> if ($. == 25);
Quote:
}
Cheers,
--Amos
--
--Amos Shapira (Jumper Extraordinaire) | "Of course Australia was marked for
| glory, for its people had been chosen
| -- Anonymous