Sed command works on Linux, but not on OS X

2020-08-11 11:08发布

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.

标签: linux macos sed
3条回答
Fickle 薄情
2楼-- · 2020-08-11 11:37

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

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-08-11 11:40

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

查看更多
女痞
4楼-- · 2020-08-11 11:46

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.

查看更多
登录 后发表回答