I ran into this problem about a month ago with a bash script that I was writing on my MacBook Pro. For some reason, sed in place editing with ‘sed -i‘ just wouldn’t work, no matter what I did, and I couldn’t find any solution online.
When running something like
sed -i 's/before/after/' test.txt
you get the error message sed: 1: "test.txt": undefined label 'est.txt'.
Eventually, it turned out to be because sed’s ‘-i‘ option takes a parameter to indicate what extension to add to the file name when making a backup. For example,
sed -i '.bak' 's/before/after/' test.txt
leaves you with two files, ‘test.txt‘ and ‘test.txt.bak‘, where ‘test.txt.bak‘ is the original version. Unlike Ubuntu and other linux versions, on OS X, this extension parameter is required. If you really know what you’re doing and don’t want a backup made, you need to provide an empty string ”.
The correct way to run this command on OSX is
sed -i '' 's/before/after/' test.txt


September 15, 2010 at 2:03 am
Thanks! I ran into this same issue. Your post helped!
September 15, 2010 at 10:33 am
Awesome! I’m glad you didn’t have to spend as much time as I did figuring it out!
November 12, 2010 at 2:55 am
Hey thanks..! Helped a lot
June 23, 2011 at 12:44 am
Thanks for sharing
August 29, 2011 at 3:33 pm
another way is:
sed ‘s/before/after/’ -i — test.txt
September 23, 2011 at 5:01 am
Thanks! This thing was driving me nuts.