On Linux, I have a bash script in which I am using sed
. In this script, I am using substitutions with "sed -i
".
Unfortunately, on MacOS, this version of sed
is not the same as Linux sed
.
I am looking for a way to use a compatible sed on Linux and MacOS, i.e having the same script for both OS.
Could you tell me if using gsed
for both OS instead of sed
allows to have a unique compatible version for this script (where gsed -i
works on both)
UPDATE 1 :
In my case, on MacOS 10.9.5, I would like to replace the line 2
of a file by the value of a variable. I tried :
a=2
sed -i'' -e "2c\$a" file.dat
but the line is replaced by "$a"
and not the value of a=2
.
Any idea ?
ps: I would like to get a Linux/MacOS portable command line.
-i
flag and do amv -f your_sed_output your_original_file
after your sed? then you don't have to monkey around with installs and prayers. – JNevillsed -i.bak '....'
and it will work same on both Lunux and MacOS – anubhavagsed
on BSD (Darwin, MacOS) or Solaris for that matter would stand for GNU sed, which is the same assed
shipped with most Linux distributions. Unfortunately, I do not think that (all) Linux distributions would also have the g prefixed name (mine doesn't) for it (btw. same storyawk
,tar
and others). – Ondrej K.Perl
can do everythingsed
can do, and far more besides, and is more orthogonal and consistent between platforms, including Solaris too. – Mark Setchellsed -e "2s|.*|$a|" file.dat
– Mark Setchell