开发者

`sed` works, but `sed -i` returns an error

This line works:

sed -r -e 's/^([^#a-z]+)localhost/\1hostname.domain hostname localhost/' /etc/开发者_JS百科hosts

But adding the itty option "i":

sed -ir -e 's/^([^#a-z]+)localhost/\1hostname.domain hostname localhost/' /etc/hosts

Results in:

sed: -e expression #1, char 60: invalid reference \1 on `s' command's RHS

Can someone tell me what's going on??


You've turned off the -r (extended syntax) option, because what you append to -i isn't more options, but an optional backup suffix. From the manpage:

-i[SUFFIX], --in-place[=SUFFIX]

   edit files in place (makes backup if extension supplied)

So just separate them:

sed -i -r -e 's/^([^#a-z]+)localhost/\1hostname.domain hostname localhost/' /etc/hosts


I think you should separate options: write -i -r and not -ir, since -i may interpret r as the suffix to append to the old unedited file, so that -r is not taken


"-ir" means something different from "-i -r" or "-ri", see the man page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜