编辑以使用 sed 打印新行两次

kyo*_*kyo 0 unix sed

我在 SSH 配置中有这个设置( /etc/ssh/sshd_config)

AllowUsers root john
Run Code Online (Sandbox Code Playgroud)

我想添加jane到行尾

AllowUsers root john jane 
Run Code Online (Sandbox Code Playgroud)

我试过了

sed -i -e '/AllowUsers/{s/:/ /g;s/.*=//;s/$/ jane/p}' /etc/ssh/sshd_config && cat /etc/ssh/sshd_config
Run Code Online (Sandbox Code Playgroud)

我一直得到这个结果

AllowUsers root john jane 
AllowUsers root john jane 
Run Code Online (Sandbox Code Playgroud)

为什么会有多余的线?


笔记

如果我以某种方式运行该命令两次

我会得到这些结果

AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
Run Code Online (Sandbox Code Playgroud)

再次运行 x3 次,将得到这个

AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
AllowUsers root john jane 
Run Code Online (Sandbox Code Playgroud)

Kam*_*Cuk 5

ps/$/ jane/p将打印模式空间。然后在循环结束时将再次打印模式空间,从而产生两行。取下p旗帜。

也就是说,只是:

sed 's/AllowUsers .*/& jane/'
Run Code Online (Sandbox Code Playgroud)

  • 那么你应该熟悉[bash中单引号和双引号之间的区别](/sf/ask/468842741/ Between-single-and-double-quotes-in-bash) (4认同)