sed正则替换内容时,碰到在Linux上正常执行的命令在MacOSX执行失败的问题,如:
sed -i 's/text/replaced/g' filename |
sed: 1: "filename": invalid command code -
google之后发现有别人碰到类似的问题,在MacOSX上需要加个空字符串在-i参数后面:
sed -i "" 's/text/relpaced/g' filename |
What I hadn’t realised is that on the Mac version of sed the
-iflag has a mandatory suffix, as described in this post.The appropriate section of the man page for sed on the Mac looks like this:
-i extension Edit files in-place, saving backups with the specified extension. If a zero-length extension is given, no backup will be saved. |
It is not recommended togive a zero-length extension when in-place editing files, as you risk corruption or partial content in situations where disk space is exhausted, etc.
Whereas on Ubuntu the suffix is optional so we see this:
-i[SUFFIX], –in-place[=SUFFIX] edit files in place (makes backup if extension supplied)In order to get around this we need to provide a blank suffix when using the
-iflag on the Mac: