2

I want to change notification_interval from 15 to 2 for block File_Check only.

I tried changing line 6th after matching File_Check line with:

sed -e '6 /File_Check/ s/15/2/g' file.txt 

but it did not work.

Here is file.txt:

define service {
    host_name                       local
    service_description             Memory
    check_command                   check_nrpe
    max_check_attempts              3
    check_interval                  5
    retry_interval                  1
    check_period                    24x7
    notification_interval           15
    contact_groups                  test
    notification_period             24x7
    notifications_enabled           0
    notification_options            w,c
    _xiwizard                       nrpe
    register                        1
    }

define service {
    host_name                       local
    service_description             File_Check
    check_command                   check_nrpe
    max_check_attempts              3
    check_interval                  5
    retry_interval                  1
    check_period                    24x7
    notification_interval           15
    contact_groups                  test
    notification_period             24x7
    notifications_enabled           0
    notification_options            w,c
    _xiwizard                       nrpe
    register                        1
    }
3
sed '/File_Check/,/contact_groups/ s/\(notification_interval\s*\)15/\12/g' file.txt

This will start in a row matching "File_Check" and end in a row matching "contact_groups" and will replace a "15" by "2" when "notification_interval" is in that row before the "15".

3

Using perl is another alternative:

perl -p00 -e 'if (/File_Check/) {s/(notification_interval\s*)15/${1}2/}' file.txt

The -00 option tells perl to read its input in "paragraph mode", blocks of text separated by blank lines. The s/// operation is applied only to the paragraph containing File_Check.

1

Alternative logic in sed:

sed -e '/File_Check/{
          :loop
          /notification_interval/!{
            N;
            b loop
          }; s/\(notification_interval\s*\)[0-9]\+/\12/
        }' your_file

Explanation

When you match File_Check, keep reading new lines and adding them to the pattern space until you match notification_interval. When you match it, make the regex substitution you need. The substitution I choose here is to substitute notification_interval\s*[0-9]\+ (notification_interval followed by any amount of white space then at least one digit) with \1 followed by 2. Now \1 means whatever was captured inside the parens \(...\) in the regex; in our case, that's notification_interval\s*.

So essentially, this looks for notification_interval followed by any amount of whitespace then a group of digits and it replaces the group of digits with 2.

  • where do i make the substitution for changing notification_interval 15 to notification_interval 2 ? also get this error sed: -e expression #1, char 88: unterminated address regex – Quay Mar 1 '16 at 20:32
  • @Quay My bad. The backslashes at the end of the line were unnecessary. I removed them and added some explanation about the regex substitution. Hope this helps. – Joseph R. Mar 1 '16 at 20:42

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.