![터미널에서 .conf 파일에 있는 고유하지 않은 키의 키 값을 어떻게 변경합니까?](https://linux55.com/image/165215/%ED%84%B0%EB%AF%B8%EB%84%90%EC%97%90%EC%84%9C%20.conf%20%ED%8C%8C%EC%9D%BC%EC%97%90%20%EC%9E%88%EB%8A%94%20%EA%B3%A0%EC%9C%A0%ED%95%98%EC%A7%80%20%EC%95%8A%EC%9D%80%20%ED%82%A4%EC%9D%98%20%ED%82%A4%20%EA%B0%92%EC%9D%84%20%EC%96%B4%EB%96%BB%EA%B2%8C%20%EB%B3%80%EA%B2%BD%ED%95%A9%EB%8B%88%EA%B9%8C%3F.png)
키/값 형식의 .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"
dir
내가 달성하고 싶은 것은 Fedora에서 블록 아래의 키 값을 변경하는 스크립트를 작성하는 것입니다 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"를 임의의 키워드로 대체하여 모든 섹션에 적용할 수 있습니다.