Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove a Host entry from an ssh config file?

Tags:

ssh

sed

The standard format of the file is:

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

Host three.example.com
  Port 4444

Knowing the host's short name, I want to remove an entire entry in order to re-add it with new details.

The closest I have got is this, except I need to keep the following Host declaration and the second search term will capture two many terms (like HostName):

sed '/Host example/,/\nHost/d'
like image 333
Ryan Avatar asked Mar 12 '23 21:03

Ryan


1 Answers

With GNU sed:

host="example"
sed 's/^Host/\n&/' file | sed '/^Host '"$host"'$/,/^$/d;/^$/d'

Output:

Host example2
  Hostname two.example.com
Host three.example.com
  Port 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

like image 195
Cyrus Avatar answered Mar 19 '23 21:03

Cyrus