0

In nagios config

define service{
          use                 generic-service
          host_name           xxx.xxx.com
          service_description xxx Status
          check_command       check_http!xxx.xxx.com -S
          }

I want to delete the entire section as given above matching regex xxx.xxx.com . How to do it using sed or any other Linux utility

1

Maybe an awk script like this:

awk -f this-script.awk my-nagios.conf > my-nagios.conf.new

# start of block
/^define/ {
  collecting = 1;
  matched = 0;
  buf = "";
}

# end of block
/}/ {
   collecting = 0;
   if (!matched) {
     print buf $0;
   };
   next;
}

# Row in bad block
/xxx.abc.com/ {
  if (collecting)
    matched = 1;
}

# Normal row
{
  if (collecting) {
    buf = buf $0 "\n";
  } else {
    print $0;
  }
}
  • The awk script collects rows of the {} block into a variable and only prints if out on the end of the block if /xxx.abc.com/ is not matched. – jkj Jan 19 '12 at 8:56
1
ed my-nagios.conf <<q
w my-bagios.conf~
g/xxx.xxx.com/ ?define?,/}/d
w
q

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.