Wednesday, August 24, 2011

In-Place replacement from command line

To replace all occurences of the word "hello" with the word "harrow" in files in the current directory, use perl's in-place substitusion from the command line as follows:

perl -pi -e 's/hello/harrow/g' *.txt

note, the 'g' at the end makes it match [greedily|globally] - ie it will to multiple matches in a single line. You can add an 'i' for case insensitivity.

Full regular expressions can be used, and the *.txt can of course be *.html, *.anything, or just *

SOURCE: http://www.spiration.co.uk/post/522/in-place-replacement-from-command-line

No comments:

Post a Comment