Search and Replace in UNIX using a Perl Script

Published by Utkarsh Patel on 08/02/09 16:32:31
Last edited on 27/04/09 21:02:14

The following search and replace method in unix is a clean and effecient perl script as unlike other scripts it does not generate any temporary files. This however maybe a disadvantage in some cases as it does not allow for error corrections.

Here are a few different ways to use this perl script:

Peform a Search and Replcae in all files:

perl -pi -e 's/SEARCH/REPLACE/g' *.* 

Peform a Search and Replace in all files with specific extention:

perl -pi -e 's/SEARCH/REPLACE/g' *.EXT 

Perform a Search and Replace in all files in all subdirectories:

perl -i -p -e 's/SEARCH/REPLACE/g' `find | grep .htm` 

About the Author

Utkarsh Patel is one of our newest members, and he could not have come at a better time. He has helped bring back stability and control to Techlicity Ventures during our rapid growth. If he isn't marvelling at a recently discovered Oracle feature or technique he is bothering the rest of the team on why we should move everything over to Oracle databases. Utkarsh complements the team well with his determination and sound research.

Bookmark and Share
Blog Widget by LinkWithin
blog comments powered by Disqus

Valid XHTML 1.0!