Macos – sed gives “illegal byte sequence” error when trying to replace a character

macososx-yosemitesedterminal

I am using the “Terminal” in Mac OS X 10.10 (Yosemite) and I am trying to delete all occurrences of a from a file, by using sed:

sed 's/a//g' file

Which gives me the following error:

sed: RE error: illegal byte sequence

sed 's/a//' file works without a problem. The error appears when I add the g modifier to the regular expression.

Best Answer

You need to add -i along with two empty ''. So it would look like this: sed -i '' 's/a//g' filename.txt

Explanation is that -i equals in-place (save it right back to the original file)

Related Question