Is there an invocation of sed
todo in-place editing without backups that works both on Linux and Mac? While the BSD sed
shipped with OS X seems to
The -i
option is not part of POSIX Sed. A more portable method would be
to use Vim in Ex mode:
ex -sc '%s/alfa/bravo/|x' file
%
select all lines
s
replace
x
save and close
This works with GNU sed, but not on OS X:
sed -i -e 's/foo/bar/' target.file
sed -i'' -e 's/foo/bar/' target.file
This works on OS X, but not with GNU sed:
sed -i '' -e 's/foo/bar/' target.file
On OS X you
sed -i -e
since the extension of the backup file would be set to -e
sed -i'' -e
for the same reasons—it needs a space between -i
and ''
.Answer: No.
The originally accepted answer actually doesn't do what is requested (as noted in the comments). (I found this answer when looking for the reason a file-e
was appearing "randomly" in my directories.)
There is apparently no way of getting sed -i
to work consistently on both MacOS and Linuces.
My recommendation, for what it is worth, is not to update-in-place with sed
(which has complex failure modes), but to generate new files and rename them afterwards. In other words: avoid -i
.
You can use sponge. Sponge is an old unix program, found in moreutils package (both in ubuntu and probably debian, and in homebrew in mac).
It will buffer all the content from the pipe, wait until the pipe is close (probably meaning that the input file is already close) and then overwrite:
From the man page:
Synopsis
sed '...' file | grep '...' | sponge file
Here's another version that works on Linux and macOS without using eval
and without having to delete backup files. It uses Bash arrays for storing the sed
parameters, which is cleaner than using eval
:
# Default case for Linux sed, just use "-i"
sedi=(-i)
case "$(uname)" in
# For macOS, use two parameters
Darwin*) sedi=(-i "")
esac
# Expand the parameters in the actual call to "sed"
sed "${sedi[@]}" -e 's/foo/bar/' target.file
This does not create a backup file, neither a file with appended quotes.
As Noufal Ibrahim asks, why can't you use Perl? Any Mac will have Perl, and there are very few Linux or BSD distributions that don't include some version of Perl in the base system. One of the only environments that might actually lack Perl would be BusyBox (which works like GNU/Linux for -i
, except that no backup extension can be specified).
As ismail recommends,
Since perl is available everywhere I just do
perl -pi -e s,foo,bar,g target.file
and this seems like a better solution in almost any case than scripts, aliases, or other workarounds to deal with the fundamental incompatibility of sed -i
between GNU/Linux and BSD/Mac.