Using sed to change strings in multiple files

Ok, one more tip for today, which makes it very simple to change text within multiple files.
Say for example that you want to change Bush to Kerry in 50+ files ending with “html”.
OK, now you could edit them, but it is too much of a hustle. Instead let’s use sed and some bash scripting:

for F in `find ./ -type f -name '*.html' -print`;
do mv $F $F.tmp;
sed 's/Bush/Kerry/g' $F.tmp > $F;
rm $F.tmp;

Be careful however, when changing “<” or “>” or any other character that has to be escaped! You have to escape it using a backslash – “\”. So for example to escape a “<” you would write “\<“. SIMPLE, right?

Questions? Here is a sed string changing script that I used.

This entry was posted in Linux. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *