Using sed to change strings in multiple files
September 15, 2007 – 21:56Ok, 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; done;
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.
