How to remove contents from file which are under bracket?

hello Friend,

In hostgroup file, i have define lots of hostgroups. I need to remove few of them without manually editing file. Need script or syntax.
I want to search particular on hostgroup_members and delete hostgoup defination of it.

for example.

define hostgroup{
        hostgroup_name          test
        alias                   test server
       hostgroup_members       test-server
}
define hostgroup{
        hostgroup_name          prd
        alias                   Prd server
       hostgroup_members       prd-server
}
define hostgroup{
        hostgroup_name          acc
        alias                   acc server
       hostgroup_members      acc-server
}

for example: suppose i delete prd-server hostgroup_members then my output will be below.

define hostgroup{
        hostgroup_name          test
        alias                   test server
       hostgroup_members       test-server
}
define hostgroup{
        hostgroup_name          acc
        alias                   acc server
       hostgroup_members      acc-server
}

Dear ghpradeep,

I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Kind regards,
Robin

awk '{$0=$0} ; $0 !~ del && NF {print $0 RS}' RS="}" del="prd-server" hostgroup

Thanks rdrtx1, above command working fine.

one query here. suppose hostgroup_members are blank. noting is there. is it possible to delete it.
example:

define hostgroup{
        hostgroup_name          acc
        alias                   acc server
       hostgroup_members     
}

How about

awk '{$0=$0} ; $0 !~ del && NF {print $0 RS}' RS="}" del="hostgroup_members *\n" file
1 Like

More correct for a Nagios-style hostgroup.cfg is probably

awk '
  /\{/ {bl++} /\}/ {bl--}
  (bl==1 && $1=="define" && $2~/^hostgroup *\{/) {
    hostgroup=1
    hbuf=""
  }
  {
    if (hostgroup==1) {
      if ($1=="hostgroup_members") {
        nh=split($2,H,/,/)
        new2=sep=""
        for (i=1;i<=nh;i++) if (host!=H) {
          new2=(new2 sep H); sep=","
        }
        if (new2!=$2) { $2=new2 }
        if (new2=="") delblk=1
      }
      hbuf=(hbuf $0 ORS)
      if (bl==0) {
        if (delblk==1) {
          delblk=0
        } else {
          printf "%s", hbuf
        }
      }
    } else { print }
  }
' host=prd-server hostgroup.cfg

It won't stumble over a { } subsection, and it will delete the given host from a list

  hostgroup_members    host1,host2,host3

Hello MadeInGermany,

thanks for code. I checked and its working while passing single hostgroups_members name
Can we use same for multiple hostgroups_members to remove by passing file which contain hostgroups_members name.
like putting above code inside for for.
anything need to change in code?

Yes, you must find a way to pass the host input into an array, like

awk -v hosts=host1,host2 'BEGIN {split (hosts,H,",")}'

Then you can cycle through them, as demonstrated here

for (i in H) print H

The hosts= needs to be passed with -v at the beginning (not at the end as I did with the host= ) so it is available in the BEGIN section.
Then in the existing code, where there is a reference to host replace it by a for loop through H.
I leave the actual implementation to you.
For clarity you can always use an explicit { block }, for instance

  for (i in H) { print H }

or

  for (i in H) {
     print H
   }

Hello MadeInGermany,

I am not much expert in Awk scripting. I have file where all hostgroup_members entry like below. I want read them in above your code and delete them.
example:
prd-server
acc-server
test-server
app-server
db-server
it will be good and one more request can u explain me code please little bit.