Use the sed command to modify the kv configuration file in Linux

Use the sed command to modify the kv configuration file in Linux

sed is a character stream editor under Unix, that is, a stream editor. It is line-oriented and processes in units of lines. At the same time, sed is non-interactive and once executed, it will process the entire file.

Daily background service configuration files are mostly in the form of key-value, such as ini files, toml files or some custom configuration files. When we need to write automated scripts to change the configuration files in some cases, we can use the shell's sed command to perform regular matching and quick modification, which is very simple and fast, reducing the tediousness of writing in a lot of "high-level languages". The following mainly lists two common configuration changes and command reference examples:

Configuration file test.conf for testing

$ cat test.conf 
max.connections = 100
test.log_path = "/tmp/test.log"
fsync=on

How to quote values

#!/bin/bash
CONF=test.conf
set_key_value() {
  local key=${1}
  local value=${2}
  if [ -n $value ]; then
    #echo $value
    local current=$(sed -n -e "s/^\($key = '\)\([^ ']*\)\(.*\)$/\2/p" $CONF) # value with single quotesif [ -n $current ];then
      echo "setting $CONF : $key = $value"
      value="$(echo "${value}" | sed 's|[&]|\\&|g')"
      sed -i "s|^[#]*[ ]*${key}\([ ]*\)=.*|${key} = '${value}'|" ${CONF}
    fi
  fi
}
set_key_value "max.connections" "1024"
set_key_value "test.log_path" "/data/logs/test.log"

Values ​​without quotes

CONF=test.conf
set_key_value() {
  local key=${1}
  local value=${2}
  if [ -n $value ]; then
    #echo $value
    local current=$(sed -n -e "s/^\($key = \)\([^ ']*\)\(.*\)$/\2/p" $CONF) # value without single quotesif [ -n $current ];then
      echo "setting $CONF : $key = $value"
      value="$(echo "${value}" | sed 's|[&]|\\&|g')"
      sed -i "s|^[#]*[ ]*${key}\([ ]*\)=.*|${key} = ${value}|" ${CONF}
    fi
  fi
}
set_key_value "fsync" "off"

Summarize

The above is what I introduced to you about how to modify the kv configuration file through the sed command under Linux. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Detailed explanation of linux sed command (recommended)
  • sed command to find the line where the keyword is located and delete the first character before it
  • Detailed explanation of the usage of Sed command and regular expression metacharacters
  • Linux tutorial on replacing strings using sed command
  • View the sed command of the system log from a certain period of time to the present
  • One shell command a day Linux text content operation series - sed command detailed explanation
  • Usage of sed command in linux
  • Summary of the use and precautions of sed command in Linux
  • Commonly used sed commands in Linux
  • How to use sed command to efficiently delete specific lines of a file

<<:  Solution to the problem that the entry cannot be found when installing mysql5.7.18

>>:  Tutorial on installing mysql5.7.18 on mac os10.12

Recommend

Sample code for automatic web page refresh and automatic jump

Automatic web page refresh: Add the following code...

Interpretation of CocosCreator source code: engine startup and main loop

Table of contents Preface preparation Go! text St...

Markup Language - Anchor

Previous: Markup Language - Phrase Elements Origin...

Use CSS to achieve circular wave effect

I often see some circular wave graphics on mobile...

Detailed explanation of JavaScript closure issues

Closures are one of the traditional features of p...

HTML uses regular expressions to test table examples

Here is an example code for using regular express...

A brief discussion on CSS blocking merging and other effects

Non-orthogonal margins When margin is used, it wi...

Can Docker become the next "Linux"?

The Linux operating system has revolutionized the...

How to query data from multiple unrelated tables and paging in Mysql

Mysql multiple unrelated tables query data and pa...

RGB color table collection

RGB color table color English name RGB 16 colors ...

Sample code for realizing book page turning effect using css3

Key Takeaways: 1. Mastering CSS3 3D animation 2. ...

The principle and implementation of js drag effect

The drag function is mainly used to allow users t...

Use Docker to create a distributed lnmp image

Table of contents 1. Docker distributed lnmp imag...