Append a line in BASH only on certain lines

0

I am working on a script to make changes to a Nagios plugin service definition using BASH. I need to append the contact group name line but only for certain service definitions. So I would start with this.

define service {
    use                     sites-service
    host_name               my_host
    service_description     check_reboot_os_updates
    check_command           check_reboot_os_updates
    contact_groups          contactgroup1
    servicegroups           MyGroup
    }
    
define service {
        use                     linux-service
        host_name               my_host
        service_description     other_description
        check_command           other_command
        contact_groups          contactgroup1
        servicegroups           MyGroup
        }

And I want to append only select contact group lines. So say I wanted to add an additional contact group to the Linux services like this.

define service {
        use                     sites-service
        host_name               my_host
        service_description     check_reboot_os_updates
        check_command           check_reboot_os_updates
        contact_groups          contactgroup1
        servicegroups           MyGroup
        }
        
define service {
        use                     linux-service
        host_name               my_host
        service_description     other_description
        check_command           other_command
        contact_groups          contactgroup1, contactgroup2
        servicegroups           MyGroup
        }

Is there a way I can do this using sed or awk or something else?

awk bash nagios sed
2021-11-23 20:51:30
2

2

with

awk '
  $1 == "use" {use = $2}
  use == "linux-service" && $1 == "contact_groups" {$0 = $0 ", contactgroup2"}
  {print}
' file

To update the file:

  • gawk -i inplace '...' file
  • awk '...' file | sponge file -- requires moreutils package
  • f=$(mktemp); awk '...' file > "$f" && mv "$f" file
2021-11-23 21:28:53
0

Using sed, if the string linux-service is unique, you can try matching from the line containing the string to the line containing the string contact_groups appending the additional group within the match.

$ sed '/linux-service/,/contact_groups/s/contact_groups.*/&, contactgroup2/' input_file
define service {
        use                     sites-service
        host_name               my_host
        service_description     check_reboot_os_updates
        check_command           check_reboot_os_updates
        contact_groups          contactgroup1
        servicegroups           MyGroup
        }

define service {
        use                     linux-service
        host_name               my_host
        service_description     other_description
        check_command           other_command
        contact_groups          contactgroup1, contactgroup2
        servicegroups           MyGroup
2021-11-23 21:19:20

That worked perfectly. The only thing I had to change was add -i after sed so it persisted. Thank you for your response.
Jim Miller

@JimMiller You're welcome. I was not aware of the in place requirement, apologies.
HatLess

no worries. You got me over the most difficult part. Have a great day.
Jim Miller

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................