【问题标题】:how to extract string between curly braces from file如何从文件中提取花括号之间的字符串
【发布时间】:2016-04-29 06:00:25
【问题描述】:

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

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

【问题讨论】:

  • 尝试使用这个 sed -n '/^define host{/,/\}$/p' bgp.cfg 虽然有效,但我认为@ritesht93 提供的答案更好

标签: python bash awk sed icinga


【解决方案1】:

如果我理解正确,您不需要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
}
$

如果你只想要大括号内的内容,你可以使用这个:

$ 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 说他只需要 host objectsservice objects,所以我认为这意味着不需要群组,即不需要 hostgroupservicegroup
  • 第一个像charm一样工作 sed -nr '/.*(\bhost\b|\bservice\b).*\{/,/\}/ p' data
猜你喜欢
  • 2021-05-25
  • 2020-02-10
  • 2020-01-26
  • 1970-01-01
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
相关资源
最近更新 更多