yq/mkdocs/write.md

251 lines
3.1 KiB
Markdown
Raw Normal View History

2017-04-13 05:36:59 +00:00
```
2018-06-20 03:42:00 +00:00
yq w <yaml_file> <path> <new value>
2017-04-13 05:36:59 +00:00
```
### To Stdout
Given a sample.yaml file of:
```yaml
b:
c: 2
```
then
```bash
2017-12-17 22:11:08 +00:00
yq w sample.yaml b.c cat
2017-04-13 05:36:59 +00:00
```
will output:
```yaml
b:
c: cat
```
### From STDIN
```bash
2017-12-17 22:11:08 +00:00
cat sample.yaml | yq w - b.c blah
2017-04-13 05:36:59 +00:00
```
### Adding new fields
Any missing fields in the path will be created on the fly.
Given a sample.yaml file of:
```yaml
b:
c: 2
```
then
```bash
2019-05-16 04:57:34 +00:00
yq w sample.yaml b.d[+] "new thing"
2017-04-13 05:36:59 +00:00
```
will output:
```yaml
b:
c: cat
d:
- new thing
```
2019-05-13 07:50:08 +00:00
### Splat
Given a sample.yaml file of:
```yaml
---
bob:
item1:
cats: bananas
item2:
cats: apples
thing:
cats: oranges
```
then
```bash
yq w sample.yaml bob.*.cats meow
```
will output:
```yaml
---
bob:
item1:
cats: meow
item2:
cats: meow
thing:
cats: meow
```
### Prefix Splat
Given a sample.yaml file of:
```yaml
---
bob:
item1:
cats: bananas
item2:
cats: apples
thing:
cats: oranges
```
then
```bash
yq w sample.yaml bob.item*.cats meow
```
will output:
```yaml
---
bob:
item1:
cats: meow
item2:
cats: meow
thing:
cats: oranges
```
2019-05-14 06:05:25 +00:00
### Array Splat
Given a sample.yaml file of:
```yaml
---
bob:
- cats: bananas
- cats: apples
- cats: oranges
```
then
```bash
yq w sample.yaml bob[*].cats meow
```
will output:
```yaml
---
bob:
- cats: meow
- cats: meow
- cats: meow
```
### Appending value to an array field
Given a sample.yaml file of:
```yaml
b:
c: 2
d:
- new thing
- foo thing
```
then
```bash
yq w sample.yaml "b.d[+]" "bar thing"
```
will output:
```yaml
b:
c: cat
d:
- new thing
- foo thing
- bar thing
```
Note that the path is in quotes to avoid the square brackets being interpreted by your shell.
2018-06-20 03:42:00 +00:00
### Multiple Documents - update a single document
Given a sample.yaml file of:
```yaml
something: else
---
b:
c: 2
```
then
```bash
yq w -d1 sample.yaml b.c 5
```
will output:
```yaml
something: else
---
b:
c: 5
```
### Multiple Documents - update all documents
Given a sample.yaml file of:
```yaml
something: else
---
b:
c: 2
```
then
```bash
yq w -d'*' sample.yaml b.c 5
```
will output:
```yaml
something: else
b:
c: 5
---
b:
c: 5
```
Note that '*' is in quotes to avoid being interpreted by your shell.
2017-04-13 05:36:59 +00:00
### Updating files in-place
Given a sample.yaml file of:
```yaml
b:
c: 2
```
then
```bash
2017-12-17 22:11:08 +00:00
yq w -i sample.yaml b.c cat
2017-04-13 05:36:59 +00:00
```
will update the sample.yaml file so that the value of 'c' is cat.
### Updating multiple values with a script
Given a sample.yaml file of:
```yaml
b:
c: 2
e:
- name: Billy Bob
```
and a script update_instructions.yaml of:
```yaml
b.c: 3
2019-05-16 04:57:34 +00:00
b.e[+].name: Howdy Partner
2017-04-13 05:36:59 +00:00
```
then
```bash
2017-12-17 22:11:08 +00:00
yq w -s update_instructions.yaml sample.yaml
2017-04-13 05:36:59 +00:00
```
will output:
```yaml
b:
c: 3
e:
- name: Howdy Partner
```
And, of course, you can pipe the instructions in using '-':
```bash
2017-12-17 22:11:08 +00:00
cat update_instructions.yaml | yq w -s - sample.yaml
```
### Values starting with a hyphen (or dash)
The flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags:
```
2017-12-17 22:11:08 +00:00
yq w -- my.path -3
```
will output
```yaml
my:
path: -3
```
{!snippets/niche.md!}