文件的标准格式是:
Host example
HostName example.com
Port 2222
Host example2
Hostname two.example.com
Host three.example.com
Port 4444
知道主机的短名称后,我想删除整个条目,以便用新的详细信息重新添加它。
我得到的最接近的是这个,除了我需要保留以下 Host 声明并且第二个搜索词将捕获两个多个词(如
HostName
):sed '/Host example/,/\nHost/d'
最佳答案
使用 GNU sed:
host="example"
sed 's/^Host/\n&/' file | sed '/^Host '"$host"'$/,/^$/d;/^$/d'
输出:主机示例2
主机名 two.example.com
主机三.example.com
端口 4444
s/^Host/\n&/
: Insert a newline before every line that begins with "Host"
/^Host '"$host"'$/,/^$/d
: delete all lines matching "Host $host" to next empty line
/^$/d
: clean up: delete every empty line
关于ssh - 如何从 ssh 配置文件中删除主机条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36110829/