Shell-Script
如何在 .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"
我想要實現的是在fedora中編寫一個腳本來更改塊下
dir
鍵的值。data
我在這裡看到了一個用於唯一鍵的類似腳本(https://stackoverflow.com/questions/2464760/modify-config-file-using-bash-script)。但不幸的是,這對我不起作用。我怎樣才能做到這一點?
假設您的文件名是 foo.conf 並且您想將 dir 值更改為“/dev/sdh”,下面的程式碼將僅替換數據部分的 dir 關鍵字。
sed -re '/^\[data\]$/,/^\[/ s/^(\s+)*(dir = .*)$/\1dir = "\/dev\/sdh"/' foo.conf
/^\[data\]$/,/^\[/
這部分使 sed 僅適用於“數據”部分。您可以將“數據”替換為任何關鍵字,使其適用於任何部分。