Search code examples
bashawksedgrepnagios

Delete multiple lines in file (Nagios configuration)


I tried to delete multiple lines from a Nagios configuration file. But I failed many times. Now I ask you for help.

Nagios configuration file:

define host{
        use                     linux-host
        host_name               vm1
        alias                   vm1
        address                 1.2.3.4    }

define host{
        use                     linux-host
        host_name               vm2
        alias                   vm2
        address                 1.2.3.4
}

define host{
        use                     linux-host
        host_name               vm3
        alias                   vm3
        address                 1.2.3.4
}

I need a general command which allows me to delete the whole text block (define host{.*}). E.g. for "vm2". The text blocks for vm1 and vm3 shouldn't be deleted.

Do you have an idea to do it?

Thank you in advance. Tzzaetaynzz


Solution

  • if blocks are separated by empty line and there is no empty line within block:

    awk -v RS="" -v ORS="\n\n" '!/vm2/' file
    

    Note that I used vm2 as a pattern, you can refine it for your needs. For example: host_name +vm1 or whatever.