Find and Replace on OS X without Sed

Last modified: 
Wednesday, August 12th, 2015

You may find the classic sed find and replace idiom doesn't work on Mac OSX owing to differences in GNU versus BSD syntax as well as character handling with "g" flag enabled.

sed -i '' 's,find,replace,g' file.txt

If the above is giving you errors like "sed: RE error: illegal byte sequence" and so forth, you can try using Perl instead. Yes, I said, "Perl."

perl -pi -w -e 's,find,replace,g;' file.txt

The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.