Bulk Add/Replace from Command Line

I had to do this like 5 times today, so I thought I’d put it out there (so I can reference it).

Note: you might want to backup your stuff, just in case you screw up your regular expression.

Basically, I have a bunch of files that need to have changes, such as email address, or include paths.

So, for example, in one I had to change a bunch of email addresses so I used `sed`. To do this on one file you would simply run the sed command directly:
sed -e s/info@olddomain.com/info@newdomain.com/g -i file.html
-e tags the regular expression you’re using
-i means we’re editing the files inline (not outputting the results to standard out)
Don’t forget g, because this will ensure that the expression is greedy and will find all the instances of this expression.

Of course, you probably want to run this on a bunch of files, so you would use `find`, because it’s awesome:
find -name "*.html" -exec sed -e s/info@olddomain.com/info@newdomain.com/g -i {} \;

EDIT: I have found that Macs have an issue with the inline command (-i). You need to use `-i ”` to give the inline an empty extension. So, the command above would be:

find -name “*.html” -exec sed -e s/info@olddomain.com/info@newdomain.com/g -i ” {} \;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s