8

Tip #153   In-place multiple-file search and replace

Regular expression search and replace on files "in-place" can be done with perl:

perl -pi -e 's/foobar/fooBar/g' file1 file2 file3

For example, here's how to change a package name for an entire Java source tree from net.widgets.* to com.widgets.*:

mv src/net src/com
find src -name '*.java' -print0 | xargs -0 perl -pi -e 's/^package net.widgets./package com.widgets./'

To have perl make backups of the original files first, follow the -i option with a suffix to append.
 
  • TAGS: