Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

search and replace

20 views
Skip to first unread message

sunil

unread,
Nov 4, 2003, 10:19:16 PM11/4/03
to

I've found a number of scripts on the web
using sed or perl, that can search a number of
files in a directory, and replace it with a
provided string.

I would like do such a search and replace operation
that goes into subdirectories,
and replaces a block of text that spans many lines,
and replace it with a string.

I looked at rpl, but couldn't figure out how
to use it to replace a text block.

Can anyone help?
Thanks

Carsten Schmitz

unread,
Nov 5, 2003, 3:46:05 AM11/5/03
to
>
> I would like do such a search and replace operation
> that goes into subdirectories,
> and replaces a block of text that spans many lines,
> and replace it with a string.

I use a perl one-liner combined with find, like this:

perl -pi -e s#text1#text2# `find . -name "*.php"`

This goes into each *.php file in every subdirectory under the current
dir, and replaces text1 with text2. You can replace *.php with *.html
or * obviously. If you need slashes in the substitution (e.g. to alter
a pathname in multiple html files), you need to mask with a backslash,
such as s#var#var\/log#

Oh, and I think (no guarantee though, try it) it also creates some
kind of .bak files so you have backups, just in case.

sunil

unread,
Nov 5, 2003, 8:08:50 AM11/5/03
to
Carsten Schmitz wrote:

s:
>
> perl -pi -e s#text1#text2# `find . -name "*.php"`
>
> This goes into each *.php file in every subdirectory under the current
> dir, and replaces text1 with text2. You can replace *.php with *.html
> or * obviously. If you need slashes in the substitution (e.g. to alter
> a pathname in multiple html files), you need to mask with a backslash,
> such as s#var#var\/log#
>
> Oh, and I think (no guarantee though, try it) it also creates some
> kind of .bak files so you have backups, just in case.

How do I use the provided script to replace a string
that spans many lines?

Noi

unread,
Nov 5, 2003, 10:34:34 AM11/5/03
to

You might look at sed examples. Something like:

sed s/replace this/with this/ filename
sed "s/_samba/_samba\/\samba/" filename

0 new messages