Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Modify the configuration file of kv mode through sed command under Linux

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Sed is a character stream-oriented editor under unix, namely stream editor, which is line-oriented and processed in units of behavior. At the same time, sed is non-interactive, and once executed, the entire file will be processed.

Most of the daily backend service configuration files exist 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 modify them quickly by regular matching through the sed command of shell, which is very simple and fast, and reduces the complexity of writing many "high-level languages". Two common configuration changes and command reference examples are listed below:

Profile test.conf for testing

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

Value in quotation marks

#! / bin/bashCONF=test.confset_key_value () {local key=$ {1} local value=$ {2} if [- n $value]; then # echo $value local current=$ (sed-n-e "s / ^\ ($key='\)\ ([^'] *\)\ (. *\) $/\ 2echo p" $CONF) # value with single quotation marks if [- 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 "

The way values are not in quotation marks

CONF=test.confset_key_value () {local key=$ {1} local value=$ {2} if [- n $value]; then # echo $value local current=$ (sed-n-e "s / ^\ ($key=\)\ ([^'] *\)\ (. *\) $/\ 2echo p" $CONF) # value without single quotation marks if [- 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"

Summary

The above is the editor introduced to you under the Linux through the sed command to modify the kv configuration file, I hope to help you, if you have any questions, please leave me a message, the editor will reply to you in time. Thank you very much for your support to the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report