| |
|
 |
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 repetative 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 occurance 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 occurance of http://asite.com/ to make the links relative. Perl makes this possible with a simple command.
The command below will remove every occurance 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"`
|
Comment List
| Topic: |
Author: |
Time: |
|
Modifying a .obj file using perl
|
SHilpa Desai
|
25.05.2004 01:38
|
|
hi,
I want to modify a .obj file using perl script or even a perl command. I am running the command on DOS prompt.
I want to replace every occurence of 1A with 1B
i tried the following command:
perl -pi.bak -e 's/1A/1B/g' test.obj
this command, removes all the contents of obj from the first occurence of 1A i.e from the first '1A' onwards, the obj file contents gets removed.
i want all the contents of .obj file. i just want to replace 1A by 1B
Thanks in advance
Shilpa
|
|
sed , the Swiss Army Knife of text file manipulation!
|
mark brandish
|
15.03.2001 11:40
|
|
If you want to do more advanced stuff use sed.
Originally written and designed for Unix, sed has been ported over to CP/M, MS-DOS, Windows 9x/NT, OS/2, and other operating systems.
Basically, sed will change hunks of text on the fly, without making you open up a screen, push a cursor around, and press DELETE or INSERT or ENTER or function keys.
For more info try:
http://www.cornerstonemag.com/sed/
and here for some oneliners:
http://www.cornerstonemag.com/sed/sed1line.txt
Word of warning though:
`` Despite the tons of examples and docs, sed is voodoo. Damned cool voodoo, but still voodoo. ''
(borrowed from Brian Moore's description of Apache's mod-rewrite)
|
|
 |
|
|