Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

ed my-nagios.conf <<q
w my-bagios.conf~
g/xxx.xxx.com/ ?define?,/}/d
w
q
share|improve this answer

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;
  }
}
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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