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

vue-router hook function implements routing guard

Table of contents Overview Global hook function R...

Some methods to optimize query speed when MySQL processes massive data

In the actual projects I participated in, I found...

Vue + OpenLayers Quick Start Tutorial

Openlayers is a modular, high-performance and fea...

Introduction to MySQL MHA operation status monitoring

Table of contents 1. Project Description 1.1 Back...

Optimize MySQL with 3 simple tweaks

I don't expect to be an expert DBA, but when ...

Implementing a simple Gobang game with native JavaScript

This article shares the specific code for impleme...

How to use vue filter

Table of contents Overview Defining filters Use o...

Packetdrill's concise user guide

1. Packetdrill compilation and installation Sourc...

Dynamic SQL statement analysis in Mybatis

This article mainly introduces the dynamic SQL st...

Some CSS questions you may be asked during an interview

This article is just to commemorate those CSS que...

How to display small icons in the browser title bar of HTML webpage

Just like this effect, the method is also very si...

Vue implements scroll loading table

Table of contents Achieve results Rolling load kn...

Build nginx virtual host based on domain name, port and IP

There are three types of virtual hosts supported ...

Chrome monitors cookie changes and assigns values

The following code introduces Chrome's monito...