Include file1 at a particular point in file2 
Author Message
 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);



--Amos Shapira (Jumper Extraordinaire) | "Of course Australia was marked for
                                       |  glory, for its people had been chosen

                                       |                         -- Anonymous

Thu, 17 Jul 1997 23:11:18 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Appending to File1 from File2

2. Need FILE1 copied to FILE2 (minus the spaces, tabs, CR, or any other invisible characters)

3. writing from file1 to file2?

4. How to use wildcard with perl -pei file1.txt file2.txt > bigfile?

5. locking "rename 'file1', 'file2';"

6. fperl:ile -newer file2

7. open(FILE, "~/dir1/file1")

8. pointing to a particular line in a file - how do you do it?

9. Perl / cgi / include file a la #include

10. send arguments to included SSI-included CGI script?

11. send arguments to included SSI-included script?

12. Including Perlscripts in SHTML (Server Side Includes)


Powered by phpBB® Forum Software