Replace Strings In Text Files

I recently moved our clients off of dyn.com to ChangeIP.com for their "free" dynamic DNS needs. These are clients for whom my micro-business provides monthly remote service over TCP/IP with ssh and VNC connections. I have dozens of scripts on our support system here that had the old dyn.com domains in them for connection to the client sites when providing service each month. As I was loathe to hand edit each of these dozens of scripts I created yet another script to interactively change the string in each client's files from the old "free" domain to the new "free" ChangeIP.com domain. There are hundreds of other scripts out on the Internet to perform a similar task. But none I looked at did exactly what I wanted, so I made my own.

Added 2013/03/07 - I realized my script was missing something when I tried to use it on some files with spaces in the names. The IFS variable has been added so the for loop can handle files with spaces in them. I also wanted to add a while loop so that I could use the script to modify files with different names by interactively looping through the process until finished. Yes, I could just restart the script each time with bash history (up arrow), but that is not as cool as scripting another loop.

Here is the updated script for any of you who may need to do your own string replacement on many files.


#!/bin/bash
#
# An interactive bash shell script.
# Created by Gene Alexander of ERA Computers & Consulting
# http://www.eracc.com/
#
# This script by Gene Alexander is licensed under a
# Creative Commons Attribution-ShareAlike 3.0 Unported License.
# http://creativecommons.org/licenses/by-sa/3.0/deed.en_US
#
# Version 0.2 - 2013/03/07

echo "Interactively replaces strings in text files using bash and sed."
echo "This script uses the sed (g)lobal replace. If you do not want"
echo "(g)lobal replacement, quit now with Ctrl C."
echo
echo "This script also has sed edit the files IN PLACE. Once you okay"
echo "the string replacement, it will be done. So be SURE you have"
echo "entered your OLD string, NEW string and FILENAME correctly. You"
echo "get one chance to review your entries before the replacement is"
echo "done. If you do it wrong, the world may end as we know it. The"
echo "other result will be that you have to run this script again to"
echo "fix your blunder."
echo
doit="y"
while [ $doit = "y" ]
do
	read -p "Enter the OLD string: "
	oldstring=$REPLY
	echo "You entered: $oldstring"
	read -p "Enter the NEW string: "
	newstring=$REPLY
	echo "You entered: $newstring"
	echo "Enter the FILENAME to modify (wildcards are okay)"
	read -p "Filenames with spaces will be handled as well: "
	filename=$REPLY
	echo "You entered: $filename"
	echo
	echo "You are replacing $oldstring with $newstring in $filename"
	echo "Press Enter or insert n to abort."
	read -p "Is this correct? (y/n): "
	answer=$REPLY
	case $answer in
		y|Y)
			oldIFS=$IFS
			IFS=$(echo -en "\n\b")
			for f in $filename
			do
				sed -i "s/$oldstring/$newstring/g" $f
			done
			IFS=$oldIFS;;
		n|N)
			exit 0;;
		*)
			exit 0;;
	esac
	echo
	read -p "Do you need to process more files? (y/n) "
	doit=$REPLY
done
 

Note that grumpy old sed experts may say something similar to, "Just use sed, stupid! It is just one line!" To which I respond that I write scripts and share them here because I know many Linux users are not script experts. In this case they are also not likely to be sed experts. The scripts I share here are to show how to get things done with shell scripts. Someone seeing this script may get ideas for his own scripting needs, regardless of whether or not he needs to use sed.

You may download the script here: streplace.sh

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.