I am writing a script with which the user can, for example, change an IP in a yaml
file by entering it via the keyboard. But I have problems with the sed
command, can anybody help me with this. I have only found commands that replace a string with a string incorporated in the command sed
.
Yaml-file
network:
address: [00.000.0.00]
Script
echo "Please enter the desired IP address:"
read input
ip="$input"
sed??????????????????
CodePudding user response:
With yq
(same as jq
but meant for YAML
):
yq '.network.address = "[1.1.1.1]"' file.yaml
{
"network": {
"address": "[1.1.1.1]"
}
}
This is the proper tool for this usage. Both python and go versions works here.
CodePudding user response:
If you are stuck with sed
:
sed -E 's/(address: *)\[[^][] \]/\1[1.1.1.1]/' file.yaml
or
sed 's/\(address: *\)\[.*/\1[1.1.1.1]/' file.yaml
Output
network:
address: [1.1.1.1]
Add -i
to edit in place