Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting word

Status
Not open for further replies.

teser

Technical User
Mar 6, 2001
194
US
I need to insert a word in meta tags in several html pages in my directory.

My tag looks like this:[tt]
<meta name=&quot;department&quot; content=&quot;department&quot;>
<meta name=&quot;building&quot; content=&quot;area,address,side&quot;>
<meta name=&quot;room&quot; content=&quot;number,space&quot;>
<meta name=&quot;cubicle&quot; content=&quot;part,sitting locale,phone number&quot;>
[/tt]

I need it to look like this (with the word area. added).
[tt]
<meta name=&quot;area.department&quot; content=&quot;department&quot;>
<meta name=&quot;area.building&quot; content=&quot;area,address,side&quot;>
<meta name=&quot;area.room&quot; content=&quot;number,space&quot;>
<meta name=&quot;area.cubicle&quot; content=&quot;part,sitting locale,phone number&quot;>[/tt]

 
suppose you already have those filenames in an array..

foreach $ff (@files){
open(FF,$ff);
chomp(@content=<FF>);
close(FF);

foreach $str (@content){
$str =~ s/<meta\s+name\=\&quot;/<meta\s+name\=\&quot;area\./gi
}
open(FF,&quot;>$ff&quot;);
foreach (@content){
print FF $_;
}
close(FF);
}

or some sort of.. Victor
 
I tried the above script but it didnt work but I got it to work on just
one file using the below script. Now can someone advise how I can get
this to work in a directory to search and change all the files in that directory?


[tt]
$db = 'met.txt';
open(DATA, &quot;$db&quot;) or die &quot;File does not open: $!&quot;;
@data = (<DATA>);
close (DATA);
open(DATA, &quot;>$db&quot;) or die &quot;File not open: $!&quot;;

foreach $line (@data)
{
$line =~ s/<meta name\=\&quot;/<meta name\=\&quot;area\./gi;
print DATA $line;
}
close(DATA);[/tt]
 
All you really need to do now is add in a glob of the directory you'd like to search. Assuming the files you're looking to make the changes to are all in one directory, the statement below should work.

@files = glob('c:\path\to\files');

foreach $db (@files)
{
open(DATA, &quot;$db&quot;) or die &quot;File does not open: $!&quot;;
@data = (<DATA>);
close (DATA);
open(DATA, &quot;>$db&quot;) or die &quot;File not open: $!&quot;;

foreach $line (@data)
{
$line =~ s/<meta name\=\&quot;/<meta name\=\&quot;area\./gi;
print DATA $line;
}
close(DATA);
}
 


I tried the above script and keep getting the following message:
File does not open: Permission denied at C:\Perl\bin\met5.pl line 6.

I then added the opendir part and got the following message:
File not open: Permission denied at C:\Perl\bin\met5.pl line 9.

Here is the script:
[tt]
@files = glob('\Perl\bin\Newf');

foreach $db (@files)
{
opendir(DATA, &quot;$db&quot;) or die &quot;File does not open: $!&quot;;
@data = (<DATA>);
close (DATA);
open(DATA, &quot;>$db&quot;) or die &quot;File not open: $!&quot;;

foreach $line (@data)
{
$line =~ s/<meta name\=\&quot;/<meta name\=\&quot;area\./gi;
print DATA $line;
}
close(DATA);
}[/tt]

I have spent alot of time trying to get this to work. Please advise.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top