1

I have a .conf file in key/value format. But there may be some non-unique keys. Distinction between them is like below:

###
### [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"

What I want to achieve is to write a script in fedora to change value of dir key under data block. I saw a similar script for unique keys in here (https://stackoverflow.com/questions/2464760/modify-config-file-using-bash-script). But this does not work for me unfortunately. How can I do this?

1 Answer 1

1

Assuming your filename is foo.conf and you want to change dir value to "/dev/sdh", below code will replace dir keyword only for data section.

sed -re '/^\[data\]$/,/^\[/ s/^(\s+)*(dir = .*)$/\1dir = "\/dev\/sdh"/' foo.conf
/^\[data\]$/,/^\[/

This part makes sed works for only "data" section. You can replace "data" to any keyword to make it work for any section.

5
  • Output of sudo sed -i -e '/^\[data\]$/,/^\[/ s/^dir = .*$/dir = "/dev/sdh"/' /etc/influxdb/influxdb.conf command is sed: -e expression #1, char 41: unknown option to s'. This one sudo sed -i -e '/^\[data\]$/,/^\[/ s/^dir = .*$/dir = "new_value"/' /etc/influxdb/influxdb.conf does not produces any output but also the file content does not change.
    – JollyRoger
    Dec 6, 2019 at 12:36
  • @JollyRoger You should escape /. Try this sudo sed -i -e '/^[data]$/,/^[/ s/^dir.*$/dir = "\/dev\/sdh"/' /etc/influxdb/influxdb.conf
    – ibrahim
    Dec 6, 2019 at 12:41
  • Hey, I changed the content of the file to exactly how I wrote above and it worked. But actually there are some comment lines starting with a # or some other keys as well. These may cause to not to be changed I guess. How can I escape from them? I am updating the sample file in my answer now.
    – JollyRoger
    Dec 6, 2019 at 12:51
  • @JollyRoger It is because trailing space character. I will update my answer to work any case.
    – ibrahim
    Dec 6, 2019 at 12:54
  • Thank you so much for your answer!
    – JollyRoger
    Dec 6, 2019 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .