Python 如何从文件中提取大括号之间的字符串

Python 如何从文件中提取大括号之间的字符串,python,bash,awk,sed,icinga,Python,Bash,Awk,Sed,Icinga,我有nagios/icinga对象文件,下面有类似的内容,但很多。如何使用bash或python提取类似的对象,例如仅提取“主机对象”或“服务对象” define host{ ## extract including the "define host {....}" use generic-switch host_name bras ; alias bras-gw.example.com;

我有nagios/icinga对象文件,下面有类似的内容,但很多。如何使用bash或python提取类似的对象,例如仅提取“主机对象”或“服务对象”

define host{          ## extract including the "define host {....}"
    use             generic-switch
    host_name       bras ;
    alias           bras-gw.example.com;
    address         20.94.66.88
    hostgroups      bgp;
}

define host{    ## extract including the "define host {....} define host {....} "
    use             generic-switch
    host_name       ar1 ;
    alias           ar1.example.com;
    address         22.98.66.244
    hostgroups      bgp;
}

define servicegroup {
   servicegroup_name Premium
   alias Premium-BGP
}
define service {
   host_name                ar0
   service_description      Get-Speed- BGP-INTL dsdf34
   check_command            check_bgp!secreat!10.10.40.44
   check_interval           1
   use                      generic-service
   notification_interval    0 ; set > 0 if you want to be re-notified
}

 define service {
   host_name                ar10
   service_description      Get-Speed- BGP-INTL rrdf34
   check_command            check_bgp!secreat!10.10.40.77
   check_interval           1
   use                      generic-service
   notification_interval    0 ; set > 0 if you want to be re-notified
   check_period                          24x7
           notification_period                   24x7
           contact_groups                        p2p,l2,system2,admins
           use                                   generic-service
           max_check_attempts      3
           notification_options    c,r
}
目标是从文件中提取特定的主机或服务对象

    define host{
    use             generic-switch
    host_name       ar0 ;
    alias           id6.example.net;
    address         20.24.6.22
    hostgroups      bgp;
}
define host{
    use             generic-switch
    host_name       bras ;
    alias           bras-gw.abc.com.dp;
    address         202.33.66.254
    hostgroups      bgp;
}
define host{
    use             generic-switch
    host_name       ar1 ;
    alias           ar1.abc.com;
    address         20.94.66.44
    hostgroups      bgp;
    } 
Ans:
sed-nr'/.*(\bhost\b |\b服务\b)。*\{/,/\}/p'数据文件

如@ritesht93提供的,如果我理解正确,您不需要
服务组
。对吗

您可以使用此
sed
命令:

$ sed -nr '/.*(\bhost\b|\bservice\b).*\{/,/\}/ p' data 
define host{          ## extract including the "define host {....}"
    use             generic-switch
    host_name       bras ;
    alias           bras-gw.example.com;
    address         20.94.66.88
    hostgroups      bgp;
}
define host{    ## extract including the "define host {....} define host {....} "
    use             generic-switch
    host_name       ar1 ;
    alias           ar1.example.com;
    address         22.98.66.244
    hostgroups      bgp;
}
define service {
   host_name                ar0
   service_description      Get-Speed- BGP-INTL dsdf34
   check_command            check_bgp!secreat!10.10.40.44
   check_interval           1
   use                      generic-service
   notification_interval    0 ; set > 0 if you want to be re-notified
}
 define service {
   host_name                ar10
   service_description      Get-Speed- BGP-INTL rrdf34
   check_command            check_bgp!secreat!10.10.40.77
   check_interval           1
   use                      generic-service
   notification_interval    0 ; set > 0 if you want to be re-notified
   check_period                          24x7
           notification_period                   24x7
           contact_groups                        p2p,l2,system2,admins
           use                                   generic-service
           max_check_attempts      3
           notification_options    c,r
}
$
如果只希望大括号内的内容,可以使用以下选项:

$ sed -nr '/.*(\bhost\b|\bservice\b).*\{/,/\}/ p' data | sed -r '/^\s*define.*\{/d;s/\}/\n/g'
    use             generic-switch
    host_name       bras ;
    alias           bras-gw.example.com;
    address         20.94.66.88
    hostgroups      bgp;


    use             generic-switch
    host_name       ar1 ;
    alias           ar1.example.com;
    address         22.98.66.244
    hostgroups      bgp;


   host_name                ar0
   service_description      Get-Speed- BGP-INTL dsdf34
   check_command            check_bgp!secreat!10.10.40.44
   check_interval           1
   use                      generic-service
   notification_interval    0 ; set > 0 if you want to be re-notified


   host_name                ar10
   service_description      Get-Speed- BGP-INTL rrdf34
   check_command            check_bgp!secreat!10.10.40.77
   check_interval           1
   use                      generic-service
   notification_interval    0 ; set > 0 if you want to be re-notified
   check_period                          24x7
           notification_period                   24x7
           contact_groups                        p2p,l2,system2,admins
           use                                   generic-service
           max_check_attempts      3
           notification_options    c,r


$

回答得好!但问题不在于获取服务和组的括号内的内容吗?@Inian OP说他只需要
主机对象
服务对象
,因此我认为这意味着不需要任何组,即不需要
主机组
和不
服务组
第一个组的工作方式与charm sed-nr'/.(\bhost\b|\bservice\b)类似/p'datatrusted这个sed-n'/^define host{/,/\}$/p'bgp.cfg虽然有效,但我认为@ritesht93提供的答案更好