【发布时间】:2018-06-23 12:58:56
【问题描述】:
我有一堆conf文件,可以分为两种:
类型 1(典型的类似 conf 文件的 ini 文件):
[server]
# Protocol (http, https)
;protocol = http
# The ip address to bind to, empty will bind to all interfaces
;http_addr = 192.168.33.2
# The http port to use
;http_port = 3000
类型 2(格式错误的 conf 文件):
###
### [http]
###
[http]
# Determines whether HTTP endpoint is enabled.
# enabled = true
# The bind address used by the HTTP service.
# bind-address = ":8080"
对于 Type1 conf 文件,我可以成功地使用带有 crudini --set filename.conf server protocol https 的 crudini p.a 之类的工具,它实际上在服务器部分下添加了一个新条目,而不是取消对现有条目的注释。只要它工作正常。
crudini 使用类型 2 文件失败并出现解析错误,因为 conf 文件不是正确的 ini 文件。对于这些我尝试使用 sed 但失败了。
我想要实现的是使用一个脚本/工具来修改这两种类型的文件。一个好的方法可能是:
- 首先找到该部分,但忽略以
;或#和部分名称开头的行 - 在该部分的所有行中搜索参数。
- 如果参数的行以
;或#开头,则替换整行(这样也去掉了空格,插入到相同的位置) - 如果没有找到该参数,则应添加该参数
- 如果参数的行以
我发现了很多脚本,其中包含很多代码,但需要一个使用 Docker conf 文件的小型解决方案。
你能帮我找到一个优雅的解决方案吗?
【问题讨论】:
-
请避免"Give me the codez" 问题。另见How much research effort is expected of Stack Overflow users?
-
为什么类型 2 格式不正确?
标签: linux awk sed dockerfile configuration-files