padmamvs
asked on
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.
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.
oops....I left out the close quote marks in the open statements :
open (FILE1,"file1.html");
open (FILE2,"file2.html");
open (FILE3,">file3.html");
open (FILE1,"file1.html");
open (FILE2,"file2.html");
open (FILE3,">file3.html");
ASKER
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?
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.
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.
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
@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
ASKER
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>
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.