[CentOS] what provices "replace" command?

Bill Campbell centos at celestial.com
Sat Jan 9 19:13:20 UTC 2010

On Sat, Jan 09, 2010, Rudi Ahlers wrote:
>I am used to using the replace command to quickly replace strings in
>file, but it's not available on some of my fresh CentOS 5.4 servers.
>"yum info replace", "yum whatprovides replace", and "yum search
>replace" doesn't show me which package(s) to install to get it. So,
>does anyone know which package to install to get the "replace"
>Google doesn't help either since the work "replace" is too common

The original replace command that I used for years was from the
Kernighan and Pike ``Unix Programming Environment'', and is a
simple shcll script that uses their ``overwrite'' command to
safely edit a file in place.  The command syntax is:

replace old new file [file ...]

MySQL created their own replace command that has different
arguments and calling sequence (not having read K&P obviously
which is one of the must-have *nix books :-).  I still use the
original K&P version, renamed ``csreplace'' to avoid conflicts
with the mysql version.  I've attached the csreplace and
overwrite scripts which can be put someplace in PATH.

A *MUCH* more flexible tool that can be used for editing in place
is Ralf Engelschall's shtool script available here:


     GNU shtool is a compilation of small but very stable and
     portable shell scripts into a single shell tool. All
     ingredients were in successful use over many years in various
     free software projects.  The compiled shtool script is intended
     to be used inside the source tree of those free software
     packages. There it can take over various (usually non-portable)
     tasks related to the building and installation of such

The only problem I've found with shtool's subst option is that
giving it a bad ``sed'' command results in zero length file(s) so
it's a good idea to test complex substitutions.

INTERNET:   bill at celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:          (206) 236-1676  Mercer Island, WA 98040-0820
Fax:            (206) 232-9186  Skype: jwccsllc (206) 855-5792

Just because you do not take an interest in politics doesn't mean politics
won't take an interest in you. -- Pericles
-------------- next part --------------
: replace:	replace str1 in files with str2, in place

# PATH=:/bin:/usr/bin:/csrel25/bin
# . /csrel25/etc/csspath	# sets path for local system

case $# in
	0|1|2)	echo 'Usage: replace str1 str2 files' 1>&2; exit 1
left="$1"; right="$2"; shift; shift
for i
	if [ -s $i ]
		overwrite $i sed "s@$left@$right at g" $i
-------------- next part --------------
: overwrite:	copy standard input to output after EOF
: final version
case $# in
	0|1)	echo 'Usage: overwrite file cmd [args]' 1>&2; exit 2
file=$1; shift
new=/tmp/overwr1.$$; old=/tmp/overwr2.$$
: clean up on interrupt
trap 'rm -f $new $old; exit 1' 1 2 15
:	collect input
if PATH=$opath "$@" >$new
	cp $file $old
	trap '' 1 2 15
	cp $new $file
	echo "overwrite: $1 failed, $file unchanged" 1>&2
	exit 1
rm -f $new $old

More information about the CentOS mailing list