터미널에서 .conf 파일에 있는 고유하지 않은 키의 키 값을 어떻게 변경합니까?

터미널에서 .conf 파일에 있는 고유하지 않은 키의 키 값을 어떻게 변경합니까?

키/값 형식의 .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"를 임의의 키워드로 대체하여 모든 섹션에 적용할 수 있습니다.

관련 정보