Sed is very powerful, I use it a lot on Linux servers I manage. Today I was working on a local git repository on Mac OSX Mountain Lion when I run into some trouble.
Usually, to replace text in a file with new text I run:
sed -i 's/Find this text/Replace with this/' file_to_replace_in.txt
While this works on Linux, it does not on Mac OSX:
sed: -i may not be used with stdin
The manpage on OSX says:
Edit files in-place, saving backups with the specified extension. If a zero-length extension is given, no backup will be saved.
Aha, it wants to save a backup file. So I changed my command to:
sed -i '.bak' 's/Find this text/Replace with this/' file_to_replace_in.txt
This works, although it leaves a backup file ‘file_to_replace_in.txt.bak’ behind. This is great if you’re not sure, but can be annoying as well. To stop it making backups you specify an empty extension, like so:
sed -i '' 's/Find this text/Replace with this/' file_to_replace_in.txt
This allows me to quickly find & replace again, like when working on Linux 🙂