Reputation: 715
I am trying to change the bind_host: localhost
to bind_host: 0.0.0.0
where the bind_host
is the 3rd layer nested value of YAML file
Current value of File
server:
application_connectors:
- type: http
port: 8989
bind_host: localhost
request_log:
appenders: []
admin_connectors:
- type: http
port: 8990
bind_host: localhost
Expected Output
server:
application_connectors:
- type: http
port: 8989
bind_host: 0.0.0.0
request_log:
appenders: []
admin_connectors:
- type: http
port: 8990
bind_host: 0.0.0.0
I am trying
awk '
/:$/{
flag=""
}
/server/{
flag=1
}
flag && NF && (/bind_host:/){
match($0,/^[[:space:]]+/);
val=substr($0,RSTART,RLENGTH);
$NF="0.0.0.0";
print val $0;
next
}
1
' config.yml
EDIT: Adding image based on answer of @inian
Upvotes: 0
Views: 4119
Reputation: 85530
If you are looking for a kislyuk/yq based solution then use the following snippet. It runs a jq
filter to update all the objects inside server
that contains bind_host
to 0.0.0.0
. The -y
flag ensures the result object is returned in YAML and not JSON
yq -y '.server |= ( with_entries (
if .value[] | select( keys[] | contains("bind_host") )
then .value[].bind_host = "0.0.0.0"
else empty end
)
)' yaml
If the modification looks as expected, use the -i
flag i.e. yq -yi
to save the modifications in-place.
Upvotes: 1
Reputation: 5678
Running
<input.yaml yq -y '((.server.application_connectors[].bind_host| select(.) ) |= gsub("localhost";"0.0.0.0") )' | \
yq -y '((.server.admin_connectors[].bind_host| select(.) ) |= gsub("localhost";"0.0.0.0") )'
you have
server:
application_connectors:
- type: http
port: 8989
bind_host: 0.0.0.0
request_log:
appenders: []
admin_connectors:
- type: http
port: 8990
bind_host: 0.0.0.0
Upvotes: 0