[ale] find/replace ever instance of X with Y in *

Jay Loden jloden at toughguy.net
Mon Jan 29 02:47:37 EST 2007


Jim Popovitch wrote:
> I periodically need a utility to change multiple instances of X with Y
> in files Z.  I know that combinations of awk/sed/grep can do some of
> this, but is there a way with one tool to do this?

Sorry if this is too late of a reply, been on the road this week. Others mentioned sed and perl methods, so I thought I'd chime in with something slightly more user friendly than sed or perl options. If you have MySQL installed, it happens to ship with a 'replace' command that does exactly what you're looking for: 

DESCRIPTION
       The replace utility program changes strings in place in files or on the standard input.

       Invoke replace in one of the following ways:

       shell> replace from to [from to] ... -- file [file] ...
       shell> replace from to [from to] ... < file

       from represents a string to look for and to represents its replacement. There can be one or more
       pairs of strings.


If that doesn't suit you, you could also use a quick shell function (or shell script) that wraps sed, which is what I did in the past when I had to do a lot of simple search and replace work in text files. This is a shell function I have loaded as part of my login process:

	strreplace () { sed -i -e "s/${1}/${2}/g" ${3}; }

HTH, 

-Jay



More information about the Ale mailing list