Read contains within { and replace the word

Hello Team,
I am looking for help to read contains from { } and replace the word.

basically, I need to use this for nagios configuration file where each host/service define within {}.

I want script or syntax to which read first word/line from {} and replace the desire word within that {}.

my nagios configuration file contains large no of hosts. I am looking for something which help me to manage easily.

Hello,

I think I get the gist of what you're saying. What would be useful though would be if you could:

  1. Provide an example entry in its original form
  2. Provide a modified copy of that entry as you would want it to look afterwards

If you can provide something like that - an entire entry in the form it exists in the file, and a modified example of how you want it to look after your script has run - that would make things easier and clearer in terms of being able to help you come up with a solution.

Hello,
Please find below example

original file:
define host{
        use                             solaris-server
        host_name                       test
        alias                           DevOps Test
        address                         test
        hostgroups                      devops-testing
}

define host{
        use                             solaris-server
        host_name                       test1
        alias                           DevOps Test1
        address                         test
        hostgroups                      devops-testing
}

modified file:
define host{
        use                             solaris-server
        host_name                       test
        alias                           DevOps Test
        address                         test
        hostgroups                      devops-production
}

define host{
        use                             solaris-server
        host_name                       test1
        alias                           DevOps Test1
        address                         test
        hostgroups                      devops-testing
}

I am getting request to change hostgroups or servicegroups of the servers. I do not want to do manually edit file. I am having file contain old hostgroups/servicegroups details with new one and hostaname. just want script or idea, how to proceed with that.

Please let me know if more details require from my side.

Hi,

OK, thanks. A follow-up question: do you always want to change every occurrence of one particular group to another ?

For example, are you always wanting to change every single instance of devops-testing to devops-production ? Or if not, what criteria determine which instances should be changed and which should be left alone ?

Hi,
I want to change instance depend upon host name. First it should match host name and then it should change the host group name or service group instance name from that group.

Hello,

Thank you for providing more information. I think though there's still a bit more that needs explained before it can be determined what the best approcah here is.

So: you say which entries need to change and what they must be changed to depends on the hostname. Now, if that's literally true - as in, what happens to every single host depends entirely on human decision, and there's no actual pattern or thing in common that determines what's to happen to each one - then this isn't really scriptable. You'd spend longer writing a script to update the file than you would updating it by hand, since there's no rhyme or reason to the changes themselves, and they all have to be handled as individual special cases.

If, however, there's some actual set of rules or a pattern that governs what needs to happen to each host, then this can certainly be done somehow. But we'd need a complete and correct description of what those rules are in order to do that.

For example: are all hosts based on the Solaris template to go to a particular group ? Are all hosts with an HTTP check defined to go to a particular group ? Et cetera, et cetera. So long is there is a set of rules you can write down that 100% accurately describe what needs to happen to each host entry, this is probaly do-able somehow, though perhaps less easily than you're imagining.

But if there really isn't a set of clearly-defined and reliable rules you can always 100% count on to define each of the changes, then the changes won't be scriptable unfortunately.