Demonstrates some examples of using yq
to process yaml files
It also can process xml here
brew info yq
brew install yq
brew upgrade yq
# using version 4.34.1 at time of writing
yq --version
# read and process
yq eval ./examples/simple.yaml
# render with debugging
yq eval -v ./examples/simple.yaml
# extract a field
yq eval .image ./examples/simple.yaml
# show internal representation of yaml
yq eval ./examples/anchors.yaml
# playing with anchors (THIS NO LONGER WORKS)
yq '. | explode(.)' ./examples/anchors.yaml
yq '. *= load("./examples/simple.yaml")' ./examples/anchors.yaml
Show an example of docker compose
overrides.
yq '. *= load("./docker-compose/docker-compose.override.yaml")' ./docker-compose/docker-compose.yaml
yq -P ./examples/broken.yaml
# replace a simple value
yq e '(.replacethis) |= "REPLACED"' ./examples/replace.yaml
# replace multiple object values
yq e '(.image) += {"repository": "docker.io/library/verdaccio.prometheus", "tag": "latest"}' ./examples/replace.yaml
# replace a substring
yq e '(.embedded) |= sub("\${APIKEY}", "theapikey")' ./examples/replace.yaml
# replace a value in an array
yq e '(.service[]) |= sub("\${APIKEY}", "theapikey")' ./examples/replace.yaml
# add a key value to an array
yq e '.extraEnvVars += [{"name": "VERDACCIO_PORT", "value": "4873"}]' ./examples/replace.yaml
# replace a value in a map
yq e '(.datasources.jsonData[]) |= sub("\${AWS_ROLE_ARN}", "arn:aws:iam::0000000000000:role/myrole")' ./examples/replace.yaml
# replace a value in a map for a specific key
yq e '(.datasources.jsonData["assumeRoleArn"]) |= "arn:aws:iam::0000000000000:role/myrole"' ./examples/replace.yaml
Take an array defined in yaml and loop over it in bash.
# run the example script
./read_into_bash_array.sh
# read in json and output yaml
yq e --output-format=yaml ./json/images.json
When converting to xml we're flattening attributes into the node heirarchy.
# xml to json (prettify the xml)
cat ./xml/results.hurl.xml | xmllint --format - | sponge ./xml/results.hurl.xml
yq e --output-format=json ./xml/results.hurl.xml
# get most recent stats from test results (TODO: replace the jq bit with yq)
yq e --output-format=json ./xml/results.hurl.xml | jq -c '.testsuites.testsuite[-1] | {"total":."+@tests", "errors": ."+@errors","failures":."+@failures"}'
# simple conversion of csv file to json
yq -o=json ./csv/simple.csv
# it seems to be broken at converting strings with : in them even if they are quoted.
yq -o=json ./csv/broken.csv
yq -o=json ./csv/bug.csv