Perl Recursive Search and Replace

23 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?

Created by Anonymous 20 weeks 6 days ago
  Tags:

Answer(s):

1 point

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

Created by Anonymous

Post your Answer

  • Lines and paragraphs break automatically.
  • 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 <% ... %>.
  • Links to specified hosts will have a rel="nofollow" added to them.

More information about formatting options