Write
Updates all the matching nodes of path expression in a yaml file to the supplied value.
yq w <yaml_file> <path_expression> <new value>See docs for path expression and value parsing for more details, including controlling quotes and tags.
Basic
Given a sample.yaml file of:
b:
  c: 2then
yq w sample.yaml b.c catwill output:
b:
  c: catUpdating files in-place
yq w -i sample.yaml b.c catwill update the sample.yaml file so that the value of 'c' is cat.
From STDIN
cat sample.yaml | yq w - b.c blahAdding new fields
Any missing fields in the path will be created on the fly.
Given a sample.yaml file of:
b:
  c: 2then
yq w sample.yaml b.d[+] "new thing"will output:
b:
  c: cat
  d:
    - new thingAppending value to an array field
Given a sample.yaml file of:
b:
  c: 2
  d:
    - new thing
    - foo thingthen
yq w sample.yaml "b.d[+]" "bar thing"will output:
b:
  c: cat
  d:
    - new thing
    - foo thing
    - bar thingNote that the path is in quotes to avoid the square brackets being interpreted by your shell.
Multiple Documents
Update a single document
Given a sample.yaml file of:
something: else
---
b:
  c: 2then
yq w -d1 sample.yaml b.c 5will output:
something: else
---
b:
  c: 5Update all documents
Given a sample.yaml file of:
something: else
---
b:
  c: 2then
yq w -d'*' sample.yaml b.c 5will output:
something: else
b:
  c: 5
---
b:
  c: 5Writing Anchors
The ---anchorName flag can be used to set the anchor name of a node
Given a sample document of:
commonStuff:
    flavour: vanillaThen:
yq write sample.yaml commonStuff --anchorName=commonBitsWill yield
commonStuff: &commonBits
    flavour: vanillaWriting Aliases
The --makeAlias flag can create (or update) a node to be an alias to an anchor.
Given a sample file of:
commonStuff: &commonBits
    flavour: vanillaThen
yq write sample.yaml --makeAnchor foo commonBitsWill yield:
commonStuff: &commonBits
    flavour: vanilla
foo: *commonBitsUpdating only styles/tags without affecting values
You can use the write command to update the quoting style of nodes, or their tags, without re-specifying the values. This is done by omitting the value argument:
Given a sample document:
a:
  c: things
  d: other thingsThen
yq write sample.yaml --style=single a.*Will yield:
a:
  c: 'things'
  d: 'other things'Using a script file to update
Given a sample.yaml file of:
b:
  d: be gone
  c: 2
  e:
    - name: Billy Bob # comment over hereand a script update_instructions.yaml of:
- command: update 
  path: b.c
  value:
    #great 
    things: frog # wow!
- command: delete
  path: b.dthen
yq w -s update_instructions.yaml sample.yamlwill output:
b:
  c:
    #great
    things: frog # wow!
  e:
  - name: Billy Bob # comment over hereAnd, of course, you can pipe the instructions in using '-':
cat update_instructions.yaml | yq w -s - sample.yamlLast updated
Was this helpful?