如何从fi中提取大括号之间的字符串

2024-05-23 21:45:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有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
}

目标是从文件中提取特定的主机或服务对象

^{pr2}$

答案:sed -nr '/.*(\bhost\b|\bservice\b).*\{/,/\}/ p' datafile 由@ritesht93提供


Tags: 文件对象namehostusecheckserviceextract
1条回答
网友
1楼 · 发布于 2024-05-23 21:45:37

如果我没听错,你不需要servicegroup..对吗?在

您可以使用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
}
$

如果只希望内容位于大括号内,则可以使用以下命令:

^{pr2}$

相关问题 更多 >