如何从ssh配置文件中删除主机条目?

Rya*_*yan 4 ssh sed

该文件的标准格式是:

Host example
  HostName example.com
  Port 2222
Host example2
  Hostname two.example.com

Host three.example.com
  Port 4444
Run Code Online (Sandbox Code Playgroud)

知道主机的短名称,我想删除整个条目,以便重新添加新的详细信息.

我最接近的是这个,除了我需要保留以下主机声明,第二个搜索术语将捕获两个很多术语(如HostName):

sed '/Host example/,/\nHost/d'
Run Code Online (Sandbox Code Playgroud)

Cyr*_*rus 5

使用GNU sed:

host="example"
sed 's/^Host/\n&/' file | sed '/^Host '"$host"'$/,/^$/d;/^$/d'
Run Code Online (Sandbox Code Playgroud)

输出:

Host example2
  Hostname two.example.com
Host three.example.com
  Port 4444

s/^Host/\n&/:在以"主机"开头的每一行之前插入换行符

/^Host '"$host"'$/,/^$/d:删除与"Host $ host"匹配的所有行到下一个空行

/^$/d:清理:删除每一个空行