0

Before posting this question, I try a lot but regex is beyond my abilities and I fail couple of times, and I can't find what I need searching with Google or Stack Overflow.

Here is my sample file:

Host xmpp2
    HostName 172.18.0.42
    User root
    Port 22

Host test
    HostName 172.18.201.2
    User root
    Port 2223
    IdentityFile /Users/arash/idkey.pem

Host afra
    HostName 79.175.169.10
    User root
    Port 22

Host hv1
    HostName 172.18.0.4
    User root
    Port 22

Host hv2
    HostName 172.18.0.5
    User root
    Port 22

I want to delete for example this block

Host test
    HostName 172.18.201.2
    User root
    Port 2223
    IdentityFile /Users/arash/idkey.pem

or any other block. It's very simple if any of these text blocks is using a specific number of lines, but now it may be 4 lines or 5 lines or more so I need to remove from Host to first blank line, not a specific number of lines.

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
Arash Shams
  • 186
  • 1
  • 2
  • 12
  • Arash, please *try* to write something, rather than just describing the problem and asking for someone to write something for you. StackOverflow is valuable not because you can get free programming service here, but because it helps you get through your own programming challenges. Post some code, explain why you think it should work, show us your results whether they're an error or unexpected output. And we'll help you fix your code. – ghoti Jan 23 '16 at 07:05

1 Answers1

2
sed '/^Host test$/,/^$/d' file

Output:

Host xmpp2
    HostName 172.18.0.42
    User root
    Port 22

Host afra
    HostName 79.175.169.10
    User root
    Port 22

Host hv1
    HostName 172.18.0.4
    User root
    Port 22

Host hv2
    HostName 172.18.0.5
    User root
    Port 22

If you want to edit your file "in place" add sed's option -i.


See: The Stack Overflow Regular Expressions FAQ

Community
  • 1
  • 1
Cyrus
  • 69,405
  • 13
  • 65
  • 117