Perl Recursive Search and Replace

This Comment will be submitted for moderation and will not be accessible to other users until it has been approved.


29 points

Perl search and replace string. I use the following script to replace all occurrences of oldstring with newstring in all of the files in your current directory.

perl -p -i -e 's/oldstring/newstring/g' *

How do I replace in sub-directories as well?



2 points

Use Perl and Find as following. Perl before find or find before perl doesn't matter.

perl -p -i -e 's/oldstring/newstring/g' `find ./ -name *.html`

or

find ./ -exec perl -p -i -e ’s/oldstring/newstring/g’ {} \;

Tony

Anonymous's picture
Created by Anonymous

Post Comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.