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?
相关问题
- JQ: Select when attribute value exists in a bash a
- bash print whole line after splitting line with if
- “command not found” errors in expect script execut
- grep using grep results
- UNIX Bash - Removing double quotes from specific s
相关文章
- Check if directory exists on remote machine with s
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- BASH: Basic if then and variable assignment
- Bash script that creates a directory structure
- Test if File/Dir exists over SSH/Sudo in Python/Ba
- How can I create a “tmp” directory with Elastic Be
- Spread 'sed' command over multiple lines
A wild stab in the dark for modifying a single value:
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.
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.
Assuming that you have a file of
key=value
pairs, potentially with spaces around the=
, you can delete, modify in-place or append key-value pairs at will usingawk
even if the keys or values contain special regex sequences:in general it's easy to extract the info with grep and cut:
to update you could do something like this:
this would not maintain the order of the key-value pairs obviously. add error checking to make sure you don't lose your data.
Hope this helps someone. I created a self contained script, which required config processing of sorts.
Suppose your config file is in below format:
In your bash script, you can use:
$CONFIG_NUM
,$CONFIG_NUM2
,$CONFIG_DEBUG
is what you need.After your read the values, write it back will be easy: