Interested in contributing to the Perl community?
Can you write expert articles and news addressing the latest changes in Perl Development? If so, we are looking for you. Contact techwriters@ientry.com.

Recent Articles

A Whole New World (Wide Web)
Although Perl may not be able to produce flying carpets, blue genies, or beautiful Arab princesses, it can be used for nearly limitless other applications.

Perl's Staying Power Advertised Again
ActiveState is a company that specializes in dynamic languages, and whether or not you've heard of it, it's quite successful, with a long list of customers that includes impressive organizations like Cisco, HP, and...

10 Perl Foundation/Parrot Foundation Projects...
Not too long ago, we talked about Google's annual Summer of Code program and its potential importance to Perl. Now, that importance has been more or less confirmed, as the Perl Foundation and the...


06.29.10



Search And Replace A Block Of Text With Perl

By Michael Marr

Searching for and then replacing text is such a fundamental function in computing that I was eager to take the opportunity to do such an operation over a large website still being served via static HTML pages.

I quickly assumed that armed with the right regular expression, I could easily conquer this task. However, this quickly became a tale of defeat followed by outstanding victory with the help of Perl.

My first option was to find a *nix command line tool to run my search and replace operation. Apparently, >sed is this kind of tool. However, after spending hours trying to find a proper regular expression to do what I needed, I had to abandoned this option. The problem was that my search string contained multiple lines, and no matter how I tried, I couldn't get >sed to play nice with multiple lines. I was able to find a single line and replace it, but obviously this was not the complete operation I needed.


My next task was to use a PERL script and attempt to accomplish the same thing as >sed does. However, my various attempts to do the same was too on-point, as again I was only able to work with single lines. Luckily, I found this script. Following the instructions supplied by the author gave me a working script. However, as most things in life, this was not 100% tailored to my intended purpose. The script works great on one directory, but the site I was needing to run a search and replace on was spread across multiple directories and subdirectories. I needed to run this script across all the files recursively. Here's how I did it:

find ./ -name '*.html' -exec perl -pi -0777 sub.pl {} \;

Executing the command >find across the current directory will also search all subdirectories for any file with .html in the name. Obviously, replace '*.html' with your proper search string. Then, it executes the perl script sub.pl (from the tutorial linked above), passing into it the current file name being found via the >{} parameter. The >\; signifies that this is the end of the statement to be executed on the results of >find. The perl parameters >-pi -0777 allow the script to be ran on the actual files passed into as parameters and save the output back into the files, while also treating whitespace and breaks in a manner compatible with the regular expression searching for the multiple line string.

About the Author:
Mike Marr is a staff writer WebProNews.
About PerlProNews
PerlProNews is a collection of news and commentary designed to keep you in step with the ever evolving landscape of Perl environments. News and Advice for Perl Professionals





PerlProNews is brought to you by:

SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
PerlProNews.com SQLProNews.com
SysAdminNews DevWebPro.com
LinuxProNews.com WirelessProNews.com
CProgrammingTrends.com ITCertificationNews.com






-- PerlProNews
is an iEntry, Inc. publication --
iEntry, 2549 Richmond Rd. Lexington KY, 40509
2010 iEntry, Inc. All Rights Reserved Privacy Policy Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article



Database Forum News and Advice for Perl Professionals PerlProNews News Archives About Us Feedback PerlProNews.com About Article Archive News Downloads WebProWorld Forums iEntry Advertise Contact Jayde