Replacing text in a file with SED on macOS

Published on 2021-07-14 • Modified on 2021-07-14

In this snippet, we see how to replace text in a file with SED on macOS. Sometimes Unix commands do not behave exactly as they would when using Unix. This SED command is a good example where the file you try to modify is being emptied. To prevent that, you can use the following commands:

sed 's/APP_ENV=dev/APP_ENV=prod/g' .env > && mv .env
sed -i '' 's/APP_ENV=dev/APP_ENV=prod/g' .env

 More on Stackoverflow  Random snippet