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:
|
<<: 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
Automatic web page refresh: Add the following code...
Table of contents Preface preparation Go! text St...
Previous: Markup Language - Phrase Elements Origin...
I often see some circular wave graphics on mobile...
Closures are one of the traditional features of p...
Here is an example code for using regular express...
Non-orthogonal margins When margin is used, it wi...
The Linux operating system has revolutionized the...
Mysql multiple unrelated tables query data and pa...
RGB color table color English name RGB 16 colors ...
Key Takeaways: 1. Mastering CSS3 3D animation 2. ...
The drag function is mainly used to allow users t...
Table of contents 1. Docker distributed lnmp imag...
I just finished installing MySQL 5.7.19 in the ea...
In front-end projects, attachment uploading is a ...