Sed in Mac OS X

Simple replacement of text at the command line may be done using the program sed.  There are endless tutorials and examples available online, but I encountered a peculiar difference between sed under UNIX and sed under BSD LINUX in Mac OS X.  I wanted to replace the symbol "e" with "d" in some Matlab output (Fortran double precision scientific notation requires "d").  I tried

$ sed -i 's/e/d/g' myfile.txt

for global replacement of "e" with "d" in the file myfile.txt, but got the following error:

$ sed: 1: "myfile.txt": extra characters at the end of d command

Perplexed, I tried a number of alternative flag/regexp combo's, getting either the above error or the following:

$ sed: 1: "i": command i expects \ followed by text

Turns out, when substituting "in place" with the -i flag using the sed in Mac OS X, you are required to supply an extension for backing up the file.  I guess this is good programming practice, but it isn't the case in most of the numerous tutorials available online (apparently written for the non-Mac OS X version of sed).  

With slight modification, everything goes fine:

$ sed -i .bk 's/e/d/g' myfile.txt

(where I've put the extension in bold, though any extension will do).  Since I don't want backup files floating around, I followed this command with 

$ rm *.bk

in the bash script for automatic clean up.

P.S.  I found one forum post that alludes to this issue, too:

Comments