Replace Content of one file with Content from another File

How can i replace part of the content in a file with the part of content from another file?
For instance I want to replace the content between <form> And </form> in a file1.html with the content between <form> and </form> of the file2.html.
padmamvsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KennyIT Application ExecutiveCommented:
open (FILE1,"file1.html);
open (FILE2,"file2.html);
open (FILE3,">file3.html);

$Status=0;
while (<FILE1>)
  {
  $Read=$_;
  if ($Read =~ /<form>/i)
    {
    $Status=1;
    while (<FILE2>)
      {
      $Read=$_;
      if ($Read =~ /<form>/i)
        {
        {$Status=2);
        }
      if ($Read =~ /<\/form>/i)
        {
        $Status=3;
        seek (FILE2,0,2);
        }
      if ($Status eq 2)
        {
        print FILE3 $Read;
        }
      }
  if ($Read =~ /<\/form>/i)
    {
    $Status=0;
    }
  if ($Status eq 0)
    {
    print FILE3 $Read;
    }
close (FILE1);
close (FILE2);
close (FILE3);

print "Content-type: text/html","\n\n";
print "DONE!";

#if you want to display the contents of the new file, just open (FILE3,file3.html) and read and wwirte it to the screen after the "content-type" line.
0
KennyIT Application ExecutiveCommented:
oops....I left out the close quote marks in the open statements :

open (FILE1,"file1.html");
open (FILE2,"file2.html");
open (FILE3,">file3.html");

0
padmamvsAuthor Commented:
The code you have given is writing the content on the top of HTML page.But I don't want that.I want to replace the content Between<form> and </form>in the file1.html exactly at the same position with the lines of code between <form> and </form> from file2.html.And also with out creating third file,Can't i replace the same file?
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

monasCommented:
You can only in one situation: if the length of parts you want to change are the same. I don't believe this is your case.

Yes, perl have command line option what allows to change files "in place". But that's the final result. It is implemented in perl like making backup copy of file to be changed, opening result file, reading from copy, making changes and writing them into result file. Copy file is then deleted.
0
ozoCommented:
open F2,"<file2.html" or die "can't open file2.html: $!";
@f2 = grep /<form>/i../<\/form>/i,<F2>;
close F2;
{local $^I=''; local @ARGV=('file1.html');
    while( <> ){
       if( /<form>/i../<\/form>/i ){
            print splice @f2,0 if @f2;
       }else
            print;
       }
    }
}

# but if you're trying to parse HTML, use HTML::Parser
0
padmamvsAuthor Commented:
Mr ozo,
The expression
grep /<form>/i../<\/form>/i,<F2>;

is not working.It is not displaying data.ie @f2 is null(does not hold data)

And also When i run the script
I am getting error as
'can't do inplace edit without backup at the <file.pl>
0
maxkirCommented:
open F2,"<file2.html" or die "can't open file2.html: $!";
@lines = <F2>;

foreach $_ (@lines)
{
    if (/^(.*)<form>(.*)$/)
    {
        $replacement = $2 ."\n";
        $inside = 1;
    }
    elsif (/^(.*)<\/form>(.*)$/)
    {
        $replacement .= $1;
        $inside = 0;
    }
    elsif ( $inside )
    {
        $replacement .= $_;
    }

}
close(F2);


open F,"<file1.html" or die "can't open file1.html: $!";
@lines = <F>;
close(F);

open(F, ">file_1.html");
foreach $_ (@lines)
{
    if (/^(.*)<form>(.*)$/)
    {
        print F $1 . "<form>" . $replacement;
        $inside = 1;
    }
    elsif (/^(.*)<\/form>(.*)$/)
    {
        print F "</form>". $2. "\n";
        $inside = 0;
    }
    elsif ( !$inside )
    {
        print F;
    }

}
close(F);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.