使用bash脚本修改配置文件 [英] modify config file using bash script

查看:331
本文介绍了使用bash脚本修改配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个bash脚本来修改其中包含一串钥匙,值对的配置文件。我怎样才能读取键和发现的价值和可能修改呢?

I'm writing a bash script to modify a config file which contains a bunch of key, value pairs. How can i read the key and find the value and possibly modify it?

推荐答案

一个野生刺在黑暗中修改一个值:

A wild stab in the dark for modifying a single value:

sed -c -i "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" $CONFIG_FILE

假定目标键和重置价值不包含任何特殊字符的正则表达式,而您的键值分隔符为=。请注意,-c选项是依赖于系统的,你可能需要省略它sed的执行。

assuming that the target key and replacement value don't contain any special regex characters, and that your key-value separator is "=". Note, the -c option is system dependent and you may need to omit it for sed to execute.

有关如何做类似的替代其他提示(例如,当REPLACEMENT_VALUE中有/字符),有一些这里很好的例子。

For other tips on how to do similar replacements (e.g., when the REPLACEMENT_VALUE has '/' characters in it), there are some great examples here.

这篇关于使用bash脚本修改配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆