【问题标题】:Comment out/delete blocks in large amount of files注释掉/删除大量文件中的块
【发布时间】:2023-04-02 07:41:01
【问题描述】:

我正在尝试编写一些可以处理包含文本块的 Nagios 配置文件的东西,然后将 # 添加到每行的开头,或者删除该块。 (作为 Nagios 本身的一种批量检查删除功能)。

例如:

define service {
        service_description     Service 1
        use                     Template 1
        host_name               Host 1
        check_command           Command A
}
define service {
        service_description     Service 2
        use                     Template 1
        host_name               Host 1
        check_command           Command B
}
define service {
        service_description     Service 3
        use                     Template 1
        host_name               Host 1
        check_command           Command C
}

需要更改为这个(或等效):

define service {
        service_description     Service 1
        use                     Template 1
        host_name               Host 1
        check_command           Command A
}
#define service {
#        service_description     Service 2
#        use                     Template 1
#        host_name               Host 1
#        check_command           Command B
#}
define service {
        service_description     Service 3
        use                     Template 1
        host_name               Host 1
        check_command           Command C
}

有没有办法正则表达式匹配“定义服务{”和“}”之间的块,并包含“服务 2”或“命令“B”,并通过 sed/awk/perl 附加/删除块,等等?

谢谢。

【问题讨论】:

  • 是的。 awk 或 perl 可能比 sed 更容易阅读 :)

标签: regex sed nagios


【解决方案1】:
sed '
# take each paragraph one by one
/define service {/,/}/{
# inside paragraphe, add each line (one at a time) in buffer
   H
# if not the end of paragraphe, delete the line (from output) (and cycle to next line)
   /}/!d
# empty current line (the last of paragraphe) and swap with the whole buffer
   s/.*//;x
# if it contain Service2 (and Command B on next line) goto to label comm
   /Service2/ b comm
   /Command B/ b comm
# goto label head (so no Service2 nor command b in paragraphe)
   b head
:comm
# comment each line of paragraphe (multiline with \n as new line)
   s/\n/&#/g
: head
# remove first character (a new line due to use of H and not h on first line)
   s/.//
   }
# default behaviour that print the content
 ' YourFile

自评

【讨论】:

    【解决方案2】:

    这是一个创建你想要的匹配的正则表达式:

    /define service\s\{.*(Service 2|Command B).*\}/s
    

    您可以test it here。我没有使用 sed、awk 或 perl 的经验,因此我不会尝试创建替代品。

    【讨论】:

    • 回复有点快,缺少上下文、参数和要使用的 sed 版本。它不适用于 posix sed,也不适用于没有特定选项的 GNU sed。
    【解决方案3】:

    sed -n '/define service {/,/}/{:1;N;/\}/!b 1;/Command B\|Service 2/{s/\n/\n#/g;s/^/#/g;p;d;b 1};p;d;b 1}' file_name
    这就是它的工作原理
    选择大括号内的块;继续附加到模式空间,直到找到右大括号;在模式空间中搜索两个关键字;如果找到,则在模式空间中的每个新行前面附加一个 # 并打印内容。清除模式空间并重复证明

    【讨论】:

    • 这很好用,谢谢!在 sed 中添加了 -i 以将更改写入我的查找循环中的每个文件。
    • 在测试时,这也会删除 Nagios 配置中的所有“define host {”块。这是为什么呢?
    • @Myaá:你能举个例子吗?我不确定我是否理解了这个问题!
    猜你喜欢
    • 2016-02-07
    • 1970-01-01
    • 2013-11-18
    • 2018-02-19
    • 2013-08-14
    • 2016-02-02
    • 1970-01-01
    • 2013-01-08
    • 2014-12-11
    相关资源
    最近更新 更多