Friday, October 17, 2008

Global text replacement

One of the things I really like about Linux is the powerful file manipulation abilities it offers. Recently I needed to replace one phrase in a whole bunch of files with another. I copied the files from my Windows PC to a Linux machine and used the following shell commands to perform the replacement:

#!/bin/sh
for file in *; do
mv $file $file.old
sed 's/FINDSTRING/REPLACESTRING/g' $file.old > $file
rm -f $file.old
done

I didn't come up with the shell commands myself, but rather found them after a google search, so I can't claim credit for it. However, maybe you will find the script useful to replace text in your own files.

No comments: