I've successfully used the following sed
command to search/replace text in Linux:
sed -i 's/old_link/new_link/g' *
However, when I try it on my Mac OS X, I get:
"command c expects \ followed by text"
I thought my Mac runs a normal BASH shell. What's up?
EDIT:
According to @High Performance, this is due to Mac sed
being of a different (BSD) flavor, so my question would therefore be how do I replicate this command in BSD sed
?
EDIT:
Here is an actual example that causes this:
sed -i 's/hello/gbye/g' *
Works on both BSD & Linux
If you use the -i option you need to provide an extension for your backups.
If you have:
The command (note the lack of space between
-i
and''
and the-e
to make it work on new versions of Mac and on GNU):create 2 backup files like:
There is no portable way to avoid making backup files because is impossible to find a mix of sed commands that works on all cases:
sed -i -e ...
- does not work on OS X as it creates-e
backupssed -i'' -e ...
- does not work on OS X 10.6 but works on 10.9+sed -i '' -e ...
- not working on GNUI believe on OS X when you use -i an extension for the backup files is required. Try:
Using GNU
sed
the extension is optional.Your Mac does indeed run a BASH shell, but this is more a question of which implementation of sed you are dealing with. On a Mac sed comes from BSD and is subtly different from the sed you might find on a typical Linux box. I suggest you
man sed
.This works with both GNU and BSD versions of sed:
or with backup:
Note missing space after
-i
option! (Necessary for GNU sed)As the other answers indicate, there is not a way to use sed portably across OS X and Linux without making backup files. So, I instead used this Ruby one-liner to do so:
In my case, I needed to call it from a
rake
task (i.e., inside a Ruby script), so I used this additional level of quoting: