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
Table of contents Overview Global hook function R...
In the actual projects I participated in, I found...
Openlayers is a modular, high-performance and fea...
Table of contents 1. Project Description 1.1 Back...
I don't expect to be an expert DBA, but when ...
This article shares the specific code for impleme...
Table of contents Overview Defining filters Use o...
1. Packetdrill compilation and installation Sourc...
This article mainly introduces the dynamic SQL st...
This article is just to commemorate those CSS que...
Just like this effect, the method is also very si...
Table of contents Achieve results Rolling load kn...
There are three types of virtual hosts supported ...
When it comes to <fieldset> and <legend&...
The following code introduces Chrome's monito...