replace string with another 

Newsgroups:  comp.unix.shell
Date:        Mon, 4 Dec 2006 21:47:26 +0000
> I need to replace one word with another in all the files under a given
> directory and not its sub-directories.[...]
>
> in my cheat sheet I have the following to find all occurrence of
> "string" in all files under a given directory and its sub_directories.
> $ grep -FHwire string /path/
>
> as well as this perl command to replace strings
> # perl -pie 's/old/new/' /path-to-file
>
> but I am sure there is a more efficient/easier way than to work out a
> syntheses of the 2 commands above.

As you seem to have a GNU find:

grep -FZ old /path/* | xargs -r0 perl -pi -e 's/old/new/g'

With standard utilities:

cd /path &&
 find . ! -name . -prune -type f -exec grep -q old {} \; \
 -exec sh -c '
   cp -i "$1" "$1.back" &&
   sed 's/old/new/g' < "$1.back" > "$1" &&
   rm "$1.back"' {} {} \;

(untested).

Stephane CHAZELAS