基于:sed:替换部分行
我想修改我的sysctl.conf文件.包含的行PermitTunnel something必须更改为PermitTunnel point-to-point.
因此,在之前的帖子中,我会使用:
sed -e 's/PermitTunnel.*$/PermitTunnel point-to-point/g'
Run Code Online (Sandbox Code Playgroud)
包括在我的文件行的末尾.
既然-n没有使用,我想我应该在标准输出中接收我操作的结果.然后我执行它并得到(注意我正在使用-2-而-to-不仅仅是为了看看我是否可以根据需要修改文件,因为在这种情况下文件已经有了所需的行):
root@debian:/home/dit# sed -e 's/PermitTunnel.*$/PermitTunnel point-2-point/g'/etc/sysctl.conf
PermitTunnel point-2-point
Run Code Online (Sandbox Code Playgroud)
但后来我做了:
root@debian:/home/dit# cat /etc/sysctl.conf | grep PermitTunnel
PermitTunnel point-to-point
Run Code Online (Sandbox Code Playgroud)
如您所见,文件没有改变.我究竟做错了什么?
谢谢阅读
您命令将sysctl.conf作为输入,将stdout作为输出.您必须使用该-i选项替换"就地"
sed -i -e 's/PermitTunnel.*$/PermitTunnel point-2-point/g'/etc/sysctl.conf
Run Code Online (Sandbox Code Playgroud)
您还可以为备份文件指定后缀:
sed -i.bak -e 's/PermitTunnel.*$/PermitTunnel point-2-point/g'/etc/sysctl.conf
Run Code Online (Sandbox Code Playgroud)
来自man sed:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
Run Code Online (Sandbox Code Playgroud)
或者,您可以将stdout重定向到新文件:
sed -e 's/PermitTunnel.*$/PermitTunnel point-2-point/g'/etc/sysctl.conf > /etc/sysctl.conf.new
Run Code Online (Sandbox Code Playgroud)