An easier way to search and replace in UNIX with perl (taken from http://www.shooter.net/index.php/weblog/Item/an-easier-way-to-search-and-replace-in-unix-with-perl/)

perl -pi -e 's/search/replace/g' *.text

Here's a breakdown of how that command works:

-p Assumes an input loop around the script. It reads each line of the file and outputs it after processing

-i Activates in place editing of files

-e Indicates a single lines script

s/search/replace/g is the script or command. In this case it's a search and replace regex (regular expression). Replace the "search" and "replace" with the text to be operated upon.

*.text the filename(s) to operate on

You can also say

perl -pi.bak -e 's/search/replace/g' *.text

The .bak tells it to save unchanged copies with that extension.

Searching and replacing directory paths with slashes

Often one will search and replace a directory path which itself contains forwards slashes. This link explains how to do so within a regex:

http://www.coderetard.com/2008/11/11/sed-how-to-escape-forward-slash-with-the-right-delimiter/

 
public/useful_shell_and_perl_commands.txt · Last modified: 2009/11/19 11:45 by aguirreg
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki