Sed command works on Linux, but not on OS X

2020-08-11 10:45发布

问题:

I am using this sed command on Linux, to patch one file, and it works fine:

sed -i -r "s/(\tpublic function __call.*)/\1\n\t\treturn null;/" rb.php

But when I try this command on OS X, I am getting an error:

sed: 1: "s/(\tpublic function __ ...": \1 not defined in the RE

So, can anybody help me to make command that will work both on Linux and OS X?

By the way, I tried command like:

sed -i '' -r "s/(\tpublic function __call.*)/\1\n\t\treturn null;/" rb.php

but it doesn't work on Linux.

回答1:

For the Mac OS X sed, use -E instead of -r to get EREs. Additionally, the GNU extensions aren't there, so you'll need literal characters instead of the \t and \n metacharacters.

Or just install GNU sed, of course.



回答2:

( is standard for perl, not for sed. You should use \( instead. Mac OS X does not use GNU sed.



回答3:

I think adding -E will make it work for both.



标签: linux macos sed