【问题标题】:Sed appears to work but file doesn't change [duplicate]Sed似乎可以工作,但文件没有改变[重复]
【发布时间】:2015-03-03 01:58:21
【问题描述】:

基于:sed: Replace part of a line

我想修改我的sysctl.conf 文件。包含PermitTunnel something 的行必须更改为PermitTunnel point-to-point。

所以使用之前一位男士在帖子中所说的,我会使用:

sed -e 's/PermitTunnel.*$/PermitTunnel point-to-point/g'

包括在我的文件的行尾。

由于没有使用-n,我想我应该会以标准输出接收我的操作结果。然后我执行它并得到(注意我使用-2-而不是-to-只是为了看看我是否可以根据需要修改文件,因为在这种情况下文件已经有所需的行):

root@debian:/home/dit# sed -e 's/PermitTunnel.*$/PermitTunnel point-2-point/g'/etc/sysctl.conf 
PermitTunnel point-2-point

但我会这样做:

root@debian:/home/dit# cat /etc/sysctl.conf | grep PermitTunnel
PermitTunnel point-to-point

如您所见,文件没有改变。我做错了什么?

感谢阅读

【问题讨论】:

标签: bash sed


【解决方案1】:

您的命令将 sysctl.conf 作为输入,将 stdout 作为输出。您必须使用-i 选项替换“就地”

sed -i -e 's/PermitTunnel.*$/PermitTunnel point-2-point/g'/etc/sysctl.conf 

您还可以为备份文件指定后缀:

sed -i.bak -e 's/PermitTunnel.*$/PermitTunnel point-2-point/g'/etc/sysctl.conf 

来自 man sed:

-i[SUFFIX], --in-place[=SUFFIX]
     edit files in place (makes backup if extension supplied)

或者,您可以将标准输出重定向到新文件:

sed -e 's/PermitTunnel.*$/PermitTunnel point-2-point/g'/etc/sysctl.conf > /etc/sysctl.conf.new

【讨论】:

  • 有关信息,在某些版本的 sed(尤其是 BSD)中,-i 标志的参数是强制性的,而不是可选的。
猜你喜欢
  • 2020-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-25
  • 2015-11-05
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
相关资源
最近更新 更多