[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[ale] find/replace ever instance of X with Y in *
- Subject: [ale] find/replace ever instance of X with Y in *
- From: jloden at toughguy.net (Jay Loden)
- Date: Mon, 29 Jan 2007 02:47:37 -0500
- In-reply-to: <1169508538.16133.3.camel@localhost>
- References: <1169508538.16133.3.camel@localhost>
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