Find and Replace in directory and subdirectories using sed on Mac

by  • April 1, 2014 • Uncategorized

Here is a good example of something I had to find and replace through out many html templates that use TinyButStrong.

find ./ -type f -name ‘*.html’ -exec sed -i ” ‘s/<h1 id=”admin-header”>\[var\.\.cst\.SITENAME\] – .*<\/h1>/<!– [onload;file=’\”[var..cst.TEMPLATEPATH]\/admin\/fragments\/admin_heading.html’\”; ↓

getbody=head;comm] –>/g’ {} \;

Notice that I pass an empty string to the -i tag, because I do not want to rename the file, but a file name is required. By providing an empty string, it just saves to the original file. Also more characters have to be escaped in the find sequence than the replace. The find sequence requires all regex characters to be replaced, where the replace sequence pretty much just requires these 3 characters to be escaped: ‘&/

You’ll also notice that a single quote has to be escaped like this: ‘/”

Good luck and have fun

Leave a Reply

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