【发布时间】: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 更容易阅读 :)