Perl One-Liner
DevWebPro- For Professional Developers  

Editor's Note - 08/10/01


Hi DevWebPro Readers,

For those of you are Visual Studio developers, there is a new online magazine starting. It is called, (of all things) Visual Studio Magazine.

Here is what the magazine's editor has to say about it:

...This new magazine targets those who develop applications using the tools that ship with Visual Studio, including Visual Basic 5/6 and Visual Basic.NET, C#, SQL Server, and ASP.NET...

Here is where you can find it: VBPJ.com

Best Wishes,
Pete



» Perl One-Liner

Have you ever been in a situation where you have to replace one or two words in many text files. As a programmer or system administrator I'm sure you have been in a situation when even Emacs' search and replace command feels like doing a lot of repetitive tasks. This small tutorial will show the power of Perl with just a line of code.

Here I will give some examples of how you can use the Perl command line to do quick and dirty search and replace jobs.

If you want to know more about regular expressions read the article: Regular Expressions Explained.

In the first example we take the file file.txt and replace every occurrence of OldText with the text NewText.

$ perl -pi -e s/OldText/NewText/g file.txt

If you have several text files and want to replace some text in them you could use the powerful find command. The Perl code below shows how you can replace a text in every file called .conf.

$ perl -pi -e s/OldText/NewText/g `find . -name "*.conf"`


A Real Life Example
Let's say you make a local copy of a web site. But when you try to browse it none of the links and images work. They all refer to http://asite.com/some/path/. You then want to remove every occurrence of http://asite.com/ to make the links relative. Perl makes this possible with a simple command.

The command below will remove every occurrence of a http:// reference and let the relative path part stay. E.g. http://zez.org/images/logo.png will be replaced with images/logo.png, this will enable local browsing of the site.

$ perl -pi.bak -e s#http://.*?/##g `find . -name "*.html"`

Remember to quote
If you are matching a text like "a text" and get an error like: Substitution pattern not terminated at -e line 1. . This is because of the spaces in the text. If you quote the regexp it works fine. The example below shows how to use a regexp with spaces.

$ perl -pi.bak -e "s#a text#another text#g" text.txt

What If I Mess Up?
If you're not familiar with regular expressions or just want to be on the safe side you should make backups. This is easily done with the -pi parameter. If you write -pi.bak the original files are saved as .bak. This way you won't loose any data, and that's nice. Better to be safe than sorry.

When you want to delete the backup files just do a rm like the example below. Warning: this is a destructive delete.

$ rm -f `find . -name "*.bak"`

Author: Bård Farstad ZEZ.org
Zez is a community website for developers, graphics and content
designers. We aim to satisfy the intermediate to expert developers.
For more information please visit www.zez.org


We at the Editorial Team would like to thank all our subscribers for
reading DevWebPro. We hope you find this focus on developer issues
useful.

Peter Thiruselvam
Editor

The DevWebPro Team




Expert in your field? We're looking for ARTICLES on web site design and development, promotion, positioning, and e-commerce. Articles should be in the 700- to 800-word range and include a 2-line promotion of the author's site. Send submissions to: Pete@DevWebPro.com
 
» Subscribe For Free!

WebProNews
WebSiteNotes
WebmasterFree
NetworkNewz
NetDummy


» Webpro Links
- DevNewz

- DesignNewz

- WirelessProNews

- SearchNewz

- promotenews


» Corporate Info
Advertising
Have your ad reach thousands of webmasters and website owners. contact: Susan@DevWebPro.com
 
Business Development
Do you have a visitor database or a newsletter? iEntry is interested in exploring partnership opportunities: Business@iEntry.com

» For Our Readers
FeedBack
Send us email with comments/suggestions.
Click here
 
Print This Issue
For a print version...
Click here

» Subscribe For Free!
Text HTML


»WMF Search
Search the WEBMASTERFREE database of free software:


 

 

-- DevWebPro is an iEntry.com publication --
 © 2001 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal