`sed`有效,但`sed -i`会返回错误

时间:2011-07-26 05:24:00

标签: linux sed

我已经接近将我的头发撕掉了我的问题。

你知道,这条线有效:

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

但添加了itty选项“i”:

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

结果:

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

有人可以告诉我发生了什么吗?


更新

你们都给出了正确答案。伙计们。显然,我对那里暂时的愚蠢感到震惊。谢谢你提醒我:)。

(那就是说,@ T.J.Crowder先回答,所以我会给他勾选标记)

3 个答案:

答案 0 :(得分:3)

您已关闭-r(扩展语法)选项,因为您附加到-i的内容不是更多选项,而是可选的备份后缀。从联机帮助页:

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

   edit files in place (makes backup if extension supplied)

所以只需将它们分开:

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

答案 1 :(得分:2)

我认为你应该分开选项:写-i -r而不是-ir,因为-i可以将r解释为附加到旧未编辑文件的后缀,这样就不会使用-r

答案 2 :(得分:1)

“ - ir”表示与“-i -r”或“-ri”不同的内容,请参阅手册页。

相关问题