Data Management

Script Teez: Simple search and replace

Want a simple search and replace solution with Linux? In his latest Script Teez, Vincent Danen shows you how to use the sed application to search and replace strings in files. Simple but very useful!


This month, we are going to look at something a little different. Compared to some of the previous scripts we've written over the last few months, this one is perhaps the simplest and most useful of the bunch. I wrote this script about a year ago and use it on a regular basis, so I thought I would share it.

This is a simple bash script that makes use of the sed program. Sed is a stream editor, which means it takes input and transforms it based on user-specified criteria. Sed is a beast all unto itself, and it can be quite complicated. Fortunately, in its most basic form, sed is extremely easy to use and very useful. In this script, we will be using sed to search for one string and replace it with another.

Let's take a look at the script itself. I call this script makenew and it looks something like this:
#!/bin/sh

list = ```ls -1 *.php`''

for file in $list
do
 sed ``s/DataBase/Database/g'' $file >t.1
 mv -f t.1 $file
done


It doesn't look like much, but I find it very useful, especially for Web work. What this script does is use as input a list of files in the current directory. In the above case, it will look for all the *.php files in the current directory. Using ls -1 ensures we get only the file name itself and no extraneous information.

Then we start a loop through all of the files found by the ls command. For each file, we execute a sed command on the file, which is represented by the $file variable. In the above script, we want to change each occurrence of DataBase to Database. Since sed normally outputs the changed text to the screen, we redirect it to the temporary file t.1. Finally, we force a move of the temporary file with the changed string to the original file and then go through the loop again until there are no files left.

The script is meant to be edited manually each time you want to run it. There are really only two lines that need to be modified depending upon your circumstances. Obviously, you need to tell the ls command what files you want to modify. In the above example, we were looking for *.php files, but you might wish to edit .html or .txt files. You can use any standard ls argument here to list files.

The second command you’ll change is the sed command itself. Since sed can be quite complicated, we'll simply look at its most basic form: a search and replace tool. The syntax you want to use for sed is:
s/string_to_find/string_to_replace_with/g

In the above example, we searched for DataBase and replaced each occurrence with Database. You can do this with any kind of string; I typically use it for path changes. For instance, if I move my Web pages from /home/httpd/html to /var/www/html and make a lot of references to the path in my PHP code, I would use the following sed command in the makenew script:
sed ``s/\/home\/httpd/\/var\/www/g'' $file >t.1

Because the forward slash character has special meaning to sed (it separates the commands), you need to escape the forward slash with a back slash if you want to use it in your string. In the above example, we are replacing all occurrences of /home/httpd with /var/www. Because there are forward slashes in our path, we have to use \/home\/httpd and \/var\/www to make the proper changes.

Now, instead of manually editing any number of Web pages with that path reference, I simply run the script like this:
./makenew

and it is all taken care of automatically. By editing one file, I have saved myself from having to edit perhaps a dozen or more files. Be sure that you make the script executable prior to trying to run it by issuing the command:
chmod u+x makenew

And that's all there is to it! A very simplistic script, but a definite time-saver for anyone who might have to edit multiple files to change the same string over and over again. If you are interested in learning about the more advanced ways of using sed, be sure to read the man pages. Short books could easily be written on the many uses of sed and the man page is a great place to start.
The authors and editors have taken care in preparation of the content contained herein but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.

About Vincent Danen

Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

Editor's Picks

Free Newsletters, In your Inbox