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

标签 ssh sed

文件的标准格式是:

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/

相关文章:

linux - git push 和 git clone 有效但 mvn release :branch 无效

python - 限制 Python 输出文件大小

bash - sed 变量太长

ubuntu - 如何从远程连接终端内将文件从本地scp到远程

php - 通过 PHP 与不同用户使用 SSH

node.js - 有没有办法指定哪个 ssh key 应该用于 npm install

linux - 下一行计数器如何在 sed 中更新?

regex - Sed 通配符——替换某些字符的中间

bash 命令求和并打印列的每个元素并打印新列

php - 根据错误的凭证处理 'ssh2_auth_password()'错误