Solved

Global text edit

Posted on 2004-09-16
11
186 Views
Last Modified: 2010-03-05
Hi All

We have sometimes 100’s of web pages to edit on our server and would like a Perl script to do it if possible. The preset variables that I would like to be able to edit would be:

$FolderPath = "the/path/to/the/folder/to/edit";
$ExtentionToEdit = "htm";
$LookFor = "look for text like this and change it";
$Replace = "This is my new text for this file";

Thanxs!
0
Comment
Question by:blnukem
  • 6
  • 5
11 Comments
 
LVL 6

Expert Comment

by:holli
ID: 12074564
use File::Find;

$FolderPath = "the/path/to/the/folder/to/edit";
$ExtentionToEdit = "htm";
$LookFor = "look for text like this and change it";
$Replace = "This is my new text for this file";

find(\&wanted, $FolderPath);

sub wanted
{
      if ( $File::Find::name =~ /$ExtentionToEdit$/ && -f $File::Find::name )
      {
            system ("perl -ni.bak -e \"s/$LookFor/$Replace/; print;\" $File::Find::name\"");
      }
}
0
 

Author Comment

by:blnukem
ID: 12082574
holli

I get this error:

sh: -c: line 1: unexpected EOF while looking for matching `"'
sh: -c: line 2: syntax error: unexpected end of file

I would really like to accomplish this with the use of modules some of our serverdont have all modules installed.
0
 
LVL 6

Expert Comment

by:holli
ID: 12099728
you will have to add the shebang-line at the beginning of the script. like:

#!/usr/bin/perl

the line must point to the correct path of your perl interpreter.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:blnukem
ID: 12100095
I did

#!/usr/bin/perl

use File::Find;

$FolderPath = "/usr/local/etc/httpd/htdocs/test";
$ExtentionToEdit = "htm";
$LookFor = "Old Words";
$Replace = "New Words";

find(\&wanted, $FolderPath);

sub wanted {

     if ( $File::Find::name =~ /$ExtentionToEdit$/ && -f $File::Find::name ){
          system ("perl -ni.bak -e \"s/$LookFor/$Replace/; print;\" $File::Find::name\"");
     }
}
print "Content-type: text/html\n\n";
print "done";
0
 
LVL 6

Expert Comment

by:holli
ID: 12100274
and?
0
 

Author Comment

by:blnukem
ID: 12100278
I get this error:

sh: -c: line 1: unexpected EOF while looking for matching `"'
sh: -c: line 2: syntax error: unexpected end of file
0
 
LVL 6

Expert Comment

by:holli
ID: 12100945
you are still exexuting the script to the shell not through perl!

try typing

perl scriptname

at the command-line
0
 
LVL 6

Expert Comment

by:holli
ID: 12100968
what's the output of the following

#!/usr/bin/perl
@a = ("hello ", "from ", "perl ");
print "@a;

??
0
 

Author Comment

by:blnukem
ID: 12167370
hello from perl
0
 
LVL 6

Accepted Solution

by:
holli earned 500 total points
ID: 12167766
replace
   system ("perl -ni.bak -e \"s/$LookFor/$Replace/; print;\" $File::Find::name\"");

with
   system ("perl -ni.bak -e \"s/$LookFor/$Replace/; print;\" $File::Find::name");
0
 

Author Comment

by:blnukem
ID: 12167819
Thanxs!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question