我有一个键/值格式的 .conf 文件。但可能存在一些非唯一的键。它们之间的区别如下:
###
### [meta]
###
### Controls the parameters for the Raft consensus group that stores metadata
### about the InfluxDB cluster.
###
[meta]
# Where the metadata/raft database is stored
dir = "/var/lib/influxdb/meta"
# Automatically create a default retention policy when creating a
database.
# retention-autocreate = true
# If log messages are printed for the meta service
# logging-enabled = true
###
### [data]
###
### Controls where the actual shard data for InfluxDB lives and how it is
### flushed from the WAL. "dir" may need to be changed to a suitable place
### for your system, but the WAL settings are an advanced configuration. The
### defaults should work for most systems.
###
[data]
# The directory where the TSM storage engine stores TSM files.
dir = "/var/lib/influxdb/data"
# The directory where the TSM storage engine stores WAL files.
wal-dir = "/var/lib/influxdb/wal"
我想要实现的是在 Fedora 中编写一个脚本来更改块dir
下键的值data
。我在这里看到了一个类似的唯一键脚本(https://stackoverflow.com/questions/2464760/modify-config-file-using-bash-script)。但不幸的是,这对我不起作用。我怎样才能做到这一点?
答案1
假设您的文件名是 foo.conf 并且您想要将 dir 值更改为“/dev/sdh”,下面的代码将仅替换数据部分的 dir 关键字。
sed -re '/^\[data\]$/,/^\[/ s/^(\s+)*(dir = .*)$/\1dir = "\/dev\/sdh"/' foo.conf
/^\[data\]$/,/^\[/
这部分使 sed 仅适用于“数据”部分。您可以将“data”替换为任何关键字,使其适用于任何部分。