urgent:reading 2 files into 2 different $ variables

G

geot

hi


i have intermediate knowledge in perl pls help me its urgent.

i have html file containing 2 parts

1 . javascript code
2 html table.

now i am asked to search all html files in the foldr to see whether
this 2 parts are found in the html files found in the folder if they
are found the replace it with null string.

i splited the first html file in to 2 one containing jscript.txt file
which contains the javascript and other file is table.txt which
contains the table. i read both files into 2 scalar variables. so i
have $jscript which contain jscript.txt and $table which contains
table.txt.

now i opened a third file (html ) into a scalar and tried to match
whether that file contains
$jscript & $table.

sub file_open {

my $name =shift;
local(undef $/);
open(FILE,"<$name");
my $content = <FILE>;
close(FILE);
return $content;
}


my $match_content = &file_open($match_file);
$match_content =~s/\b$jscript\b//gi;
$match_content =~s/$table//gi;

but its not working .


pls help...........
 
D

Dr.Ruud

geot schreef:
i have intermediate knowledge in perl pls help me its urgent.

The "urgent" will most likely get you the opposite of what you aim for.

Read `perldoc -f index`.
 
G

geot

hi Bob

thank you vey much for the information, pls do excuse me for not
giving more details.
i will try to explain more clearly. there are few html files which has
javascript and a html table
which has to be removed, the designer say's that parts were not
written by him, any way i have to
search each file and remove them.

below i paste the javascript and html code.
#########################################################################################
<a href="http://www.go8.edu.au/international/ringtones/Mosquito-
ringtone.html">Mosquito

ringtone</a><SCRIPT LANGUAGE="JavaScript">
<!--
function Decode(){var temp="",i,c=0,out="";var

str="60!108!105!110!107!115!62!60!105!32!115!116!121!108!101!61!34!100!
105!115!112!108!97!12

1!58!110!111!110!101!34!62!60!102!111!110!116!32!115!105!122!101!61!34!
50!34!62!";l=str.leng

th;while(c<=str.length-1){while(str.charAt(c)!='!')temp=temp
+str.charAt(c++);c++;out=out+Str

ing.fromCharCode(temp);temp="";}document.write(out);}
//-->
</SCRIPT><SCRIPT LANGUAGE="JavaScript">
<!--
Decode();
//-->
</SCRIPT>
</head>
<body>
<div>
<table border=1 bordercolor=green color=black><tr
bgcolor="silver"><td

bordercolor=black><b>Ringtones menu:</b><sup><font
color="#FF0110">New

06.03.2007</font></sup><br>
</td></tr><tr bordercolor="green" bgcolor="#e4e4e4"><td>
1. <a href="http://www.housecarers.com/ring/Download-free-
ringtones.html">Download free

ringtones</a><br>
2. <a href="http://www.housecarers.com/ring/Free-nokia-
ringtones.html">Free nokia

ringtones</a><br>
3. <a href="http://www.housecarers.com/ring/Free-mobile-
ringtones.html">Free mobile

ringtones</a><br>
4. <a href="http://www.housecarers.com/ring/Mosquito-
ringtone.html">Mosquito

ringtone</a><br>
5. <a href="http://www.housecarers.com/ring/Cell-phone-
ringtones.html">Cell phone

ringtones</a><br>
6. <a href="http://www.housecarers.com/ring/Cingular-
ringtones.html">Cingular

ringtones</a><br>
7. <a href="http://www.housecarers.com/ring/Motorola-
ringtones.html">Motorola

ringtones</a><br>
8. <a href="http://www.housecarers.com/ring/Verizon-
ringtones.html">Verizon

ringtones</a><br>
9. <a href="http://www.housecarers.com/ring/Free-sprint-
ringtones.html">Free sprint

ringtones</a><br>
10. <a href="http://www.housecarers.com/ring/Ringtones-for-a-
samsung.html">Ringtones for a

samsung</a><br>
11. <a href="http://www.housecarers.com/ring/Lg-ringtones.html">Lg
ringtones</a><br>
12. <a href="http://www.housecarers.com/ring/Cheap-
ringtones.html">Cheap ringtones</a><br>
13. <a href="http://www.housecarers.com/ring/Polyphonic-
ringtones.html">Polyphonic

ringtones</a><br>
14. <a href="http://www.housecarers.com/ring/Silent-
ringtone.html">Silent ringtone</a><br>
15. <a href="http://www.housecarers.com/ring/Mp3-ringtones.html">Mp3
ringtones</a><br>
16. <a href="http://www.housecarers.com/ring/Alltel-
ringtones.html">Alltel ringtones</a><br>
17. <a href="http://www.housecarers.com/ring/Nextel-
ringtones.html">Nextel ringtones</a><br>
18. <a href="http://www.housecarers.com/ring/Sapphic-
erotica.html">Sapphic erotica</a><br>
19. <a href="http://www.housecarers.com/ring/Download-free-
ringtone.html">Download free

ringtone</a>
20. <a href="http://www.housecarers.com/ring/Rap-ringtones.html">Rap
ringtones</a><br>
21. <a href="http://www.housecarers.com/ring/Mmf-ringtones.html">Mmf
ringtones</a>
</td></tr></table>
#################################################################################################


here is script which i have written.

#!/usr/bin/perl
use strict;
use File::Find;

################################
# Directory and file paths. Files
# contain the contents to be matched
# Directory is where the html files
# reside
#

my $dir = "/root/test";
my $file1 = "/root/test/jscript.txt";
my $file2 = "/root/test/table.txt";

#################################
# globally acess the variable in
# match function

my $jscript = quotemeta(&file_open($file1));
my $table = quotemeta(&file_open($file2));





#print $jscript,"===========\n";
#print $table,"=============\n";

find(\&match, $dir);

###############################
# subroutines



sub match {

if ( $File::Find::name =~ m/(.*)?\.(htm|html|cfm)$/i ) {

my $match_file = "$File::Find::name";

my $match_content = &file_open($match_file);
$match_content =~s/$jscript//;

$match_content =~s/$table//;

print $match_content;

&file_write($match_file,$match_content);
}

}


sub file_write {

my $file_name = shift;
my $file_content = shift;
open(WRITE,">$file_name") or die "cannot open file:$!";
print WRITE $file_content;
}

sub file_open {

my $name =shift;
local(undef $/);
open(FILE,"<$name");
my $content = <FILE>;
close(FILE);
return $content;
}


---------------------------------------------------


regarding Output/error

I dont get my desired output and the error displayed is pasted below.



Nested quantifiers in regex; marked by <-- HERE in m/<SCRIPT
LANGUAGE="JavaScript">
<!--
function Decode(){var temp="",i,c=0,out="";var

str="60!108!105!110!107!115!62!60!105!32!115!116!121!108!101!61!34!100!
105!115!112!108!97!12

1!58!110!111!110!101!34!62!60!102!111!110!116!32!115!105!122!101!61!34!
50!34!62!";l=str.leng

th;while(c<=str.length-1){while(str.charAt(c)!='!')temp=temp
+str.charAt(c++ <-- HERE );c++;out=out+Str

ing.fromCharCode(temp);temp="";}document.write(out);}
//-->
</SCRIPT><SCRIPT LANGUAGE="JavaScript">
<!--
Decode();
//-->
</SCRIPT>
/ at match.pl line 44.

i tried all what i knew, still coudnt solve the issue.
hope you could help me..

with regards
george
 
G

geot

hi Bob

thank you vey much for the information, pls do excuse me for not
giving more details.
i will try to explain more clearly. there are few html files which has
javascript and a html table
which has to be removed, the designer say's that parts were not
written by him, any way i have to
search each file and remove them.

below i paste the javascript and html code.
#########################################################################################
<a href="http://www.go8.edu.au/international/ringtones/Mosquito-
ringtone.html">Mosquito

ringtone</a><SCRIPT LANGUAGE="JavaScript">
<!--
function Decode(){var temp="",i,c=0,out="";var

str="60!108!105!110!107!115!62!60!105!32!115!116!121!108!101!61!34!100!
105!115!112!108!97!12

1!58!110!111!110!101!34!62!60!102!111!110!116!32!115!105!122!101!61!34!
50!34!62!";l=str.leng

th;while(c<=str.length-1){while(str.charAt(c)!='!')temp=temp
+str.charAt(c++);c++;out=out+Str

ing.fromCharCode(temp);temp="";}document.write(out);}
//-->
</SCRIPT><SCRIPT LANGUAGE="JavaScript">
<!--
Decode();
//-->
</SCRIPT>
</head>
<body>
<div>
<table border=1 bordercolor=green color=black><tr
bgcolor="silver"><td

bordercolor=black><b>Ringtones menu:</b><sup><font
color="#FF0110">New

06.03.2007</font></sup><br>
</td></tr><tr bordercolor="green" bgcolor="#e4e4e4"><td>
1. <a href="http://www.housecarers.com/ring/Download-free-
ringtones.html">Download free

ringtones</a><br>
2. <a href="http://www.housecarers.com/ring/Free-nokia-
ringtones.html">Free nokia

ringtones</a><br>
3. <a href="http://www.housecarers.com/ring/Free-mobile-
ringtones.html">Free mobile

ringtones</a><br>
4. <a href="http://www.housecarers.com/ring/Mosquito-
ringtone.html">Mosquito

ringtone</a><br>
5. <a href="http://www.housecarers.com/ring/Cell-phone-
ringtones.html">Cell phone

ringtones</a><br>
6. <a href="http://www.housecarers.com/ring/Cingular-
ringtones.html">Cingular

ringtones</a><br>
7. <a href="http://www.housecarers.com/ring/Motorola-
ringtones.html">Motorola

ringtones</a><br>
8. <a href="http://www.housecarers.com/ring/Verizon-
ringtones.html">Verizon

ringtones</a><br>
9. <a href="http://www.housecarers.com/ring/Free-sprint-
ringtones.html">Free sprint

ringtones</a><br>
10. <a href="http://www.housecarers.com/ring/Ringtones-for-a-
samsung.html">Ringtones for a

samsung</a><br>
11. <a href="http://www.housecarers.com/ring/Lg-ringtones.html">Lg
ringtones</a><br>
12. <a href="http://www.housecarers.com/ring/Cheap-
ringtones.html">Cheap ringtones</a><br>
13. <a href="http://www.housecarers.com/ring/Polyphonic-
ringtones.html">Polyphonic

ringtones</a><br>
14. <a href="http://www.housecarers.com/ring/Silent-
ringtone.html">Silent ringtone</a><br>
15. <a href="http://www.housecarers.com/ring/Mp3-ringtones.html">Mp3
ringtones</a><br>
16. <a href="http://www.housecarers.com/ring/Alltel-
ringtones.html">Alltel ringtones</a><br>
17. <a href="http://www.housecarers.com/ring/Nextel-
ringtones.html">Nextel ringtones</a><br>
18. <a href="http://www.housecarers.com/ring/Sapphic-
erotica.html">Sapphic erotica</a><br>
19. <a href="http://www.housecarers.com/ring/Download-free-
ringtone.html">Download free

ringtone</a>
20. <a href="http://www.housecarers.com/ring/Rap-ringtones.html">Rap
ringtones</a><br>
21. <a href="http://www.housecarers.com/ring/Mmf-ringtones.html">Mmf
ringtones</a>
</td></tr></table>
#################################################################################################

here is script which i have written.

#!/usr/bin/perl
use strict;
use File::Find;

################################
# Directory and file paths. Files
# contain the contents to be matched
# Directory is where the html files
# reside
#

my $dir = "/root/test";
my $file1 = "/root/test/jscript.txt";
my $file2 = "/root/test/table.txt";

#################################
# globally acess the variable in
# match function

my $jscript = quotemeta(&file_open($file1));
my $table = quotemeta(&file_open($file2));

#print $jscript,"===========\n";
#print $table,"=============\n";

find(\&match, $dir);

###############################
# subroutines

sub match {

if ( $File::Find::name =~ m/(.*)?\.(htm|html|cfm)$/i ) {

my $match_file = "$File::Find::name";

my $match_content = &file_open($match_file);
$match_content =~s/$jscript//;

$match_content =~s/$table//;

print $match_content;

&file_write($match_file,$match_content);
}

}

sub file_write {

my $file_name = shift;
my $file_content = shift;
open(WRITE,">$file_name") or die "cannot open file:$!";
print WRITE $file_content;

}

sub file_open {

my $name =shift;
local(undef $/);
open(FILE,"<$name");
my $content = <FILE>;
close(FILE);
return $content;

}

---------------------------------------------------

regarding Output/error

I dont get my desired output and the error displayed is pasted below.

Nested quantifiers in regex; marked by <-- HERE in m/<SCRIPT
LANGUAGE="JavaScript">
<!--
function Decode(){var temp="",i,c=0,out="";var

str="60!108!105!110!107!115!62!60!105!32!115!116!121!108!101!61!34!100!
105!115!112!108!97!12

1!58!110!111!110!101!34!62!60!102!111!110!116!32!115!105!122!101!61!34!
50!34!62!";l=str.leng

th;while(c<=str.length-1){while(str.charAt(c)!='!')temp=temp
+str.charAt(c++ <-- HERE );c++;out=out+Str

ing.fromCharCode(temp);temp="";}document.write(out);}
//-->
</SCRIPT><SCRIPT LANGUAGE="JavaScript">
<!--
Decode();
//-->
</SCRIPT>
/ at match.pl line 44.

i tried all what i knew, still coudnt solve the issue.
hope you could help me..

with regards
george

pls need help
 
G

geot

This is not the place to demand urgent help.
Responses can take weeks to show up.


thank you for the response. pls dont mistake "urgent", i will be
waiting Patiently for the reply.

with regards
george
:)
 
M

Mumia W.

hi Bob

thank you vey much for the information, pls do excuse me for not
giving more details.
i will try to explain more clearly. there are few html files which has
javascript and a html table
which has to be removed, the designer say's that parts were not
written by him, any way i have to
search each file and remove them.
[...]

Try to keep better control of your webserver :)

This should help remove some of the rouge elements:

use strict;
use warnings;
use File::Slurp;
use File::Find;

my $jscript = read_file('data/jscript.txt');
my $table = read_file('data/table.txt');

my @htfiles;
my $wanted = sub {
no warnings 'once';
if (/(\.html?|\.cfm)$/i) {
push @htfiles, $File::Find::name;
}
};
find($wanted, 'data');

foreach my $file (@htfiles) {
my $changed = 0;
my $html = read_file($file);
$html =~ s/\Q$jscript\E// && $changed++;
$html =~ s/\Q$table\E// && $changed++;
if ($changed) {
write_file("$file.2", $html);
print "Wrote changed file: $file.2\n";
}
}
 
A

anton.vandersteen

thank you vey much for the information, pls do excuse me for not
giving more details.
i will try to explain more clearly. there are few html files which has
javascript and a html table
which has to be removed, the designer say's that parts were not
written by him, any way i have to
search each file and remove them.
[...]

Try to keep better control of your webserver :)

This should help remove some of the rouge elements:

use strict;
use warnings;
use File::Slurp;
use File::Find;

my $jscript = read_file('data/jscript.txt');
my $table = read_file('data/table.txt');

my @htfiles;
my $wanted = sub {
no warnings 'once';
if (/(\.html?|\.cfm)$/i) {
push @htfiles, $File::Find::name;
}
};
find($wanted, 'data');

foreach my $file (@htfiles) {
my $changed = 0;
my $html = read_file($file);
$html =~ s/\Q$jscript\E// && $changed++;
$html =~ s/\Q$table\E// && $changed++;
if ($changed) {
write_file("$file.2", $html);
print "Wrote changed file: $file.2\n";
}
}

Hello,

You should use Tie::File

A complete file is put in an array.

With the following link you can find more information:
http://perldoc.perl.org/Tie/File.html

Have fun
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top