Quirk running Sed on Mac

Hi all, I ran into a problem during Lingua Franca. the sed command returned an error invalid command W. Searching for an answer brought me to this

If you are on a OS X, this probably has nothing to do with the sed command. On the OSX version of sed , the -i option expects an extension argument so your command is actually parsed as the extension argument and the file path is interpreted as the command code.

Try adding the -e argument explicitly and giving '' as argument to -i :

 sed -i '' -e "s/search string/replace string/" directory/file.extention ;

It links out https://stackoverflow.com/questions/7573368/in-place-edits-with-sed-on-os-x

Figured I'd bring it up in case anyone else uses OsX and ran into the same problem?
1 Like

I had the same problem and could not make it work. It would be good if the run-through for the exercise was edited with the appropriate code for OSX.

To clarify,

sed -i’.bak’ -e ‘s/lingua-franca/lingua_franca/g’ /.txt
did not work for me. It only worked after I removed the -e argument. I do not understand exactly why, but the following worked:

sed -i’-bak’ ‘s/Lingua-Franca/Lingua Franca/g’ /.txt

I think this may be due to running the bash shell rather than the zsh shell?

It’s always useful to run man on anything that could be variable, man sed in this case would show how it would work w/ your system.

Rather than having lines that run in all systems it’d be useful if they make a passing note on accessing this help (…they might, I can’t say I’ve done this exercise).

2 Likes

Thanks, @toastedpitabread, that’s helpful! I agree that it would make sense to raise awareness in the instructions that each user needs to figure out how sed works on their system, maybe just a tip to try man sed.

I believe it’s because OSX runs BSD sed whereas the instructions appear to be using GNU sed.


1 Like

Hi all, had the same issue
tried to upgrade the bash shell by homebrew
tried it with the old and new version of bash shell
then with the old and new version of zsh shell but sed still changed anything on a mac, just print the result.

It seams that on mac “sed” is only printing out and not changing the files in general.

The only solution I found that sed is changing anything in the files is to write
sed -i “” -e
or
sed -i ‘’ -e
thank you for your tip samanthaphoenix!