2017-04-13 05:36:59 +00:00
|
|
|
```
|
2017-12-17 22:11:08 +00:00
|
|
|
yq w <yaml_file|json_file> <path> <new value>
|
2017-04-13 05:36:59 +00:00
|
|
|
```
|
2017-04-19 05:45:45 +00:00
|
|
|
{!snippets/works_with_json.md!}
|
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
|
2017-12-17 22:11:08 +00:00
|
|
|
yq w sample.yaml b.d[0] "new thing"
|
2017-04-13 05:36:59 +00:00
|
|
|
```
|
|
|
|
will output:
|
|
|
|
```yaml
|
|
|
|
b:
|
|
|
|
c: cat
|
|
|
|
d:
|
|
|
|
- new thing
|
|
|
|
```
|
|
|
|
|
2017-09-24 00:21:16 +00:00
|
|
|
### Appending value to an array field
|
|
|
|
Given a sample.yaml file of:
|
|
|
|
```yaml
|
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
d:
|
|
|
|
- new thing
|
|
|
|
- foo thing
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2018-02-27 21:07:34 +00:00
|
|
|
yq w sample.yaml "b.d[+]" "bar thing"
|
2017-09-24 00:21:16 +00:00
|
|
|
```
|
|
|
|
will output:
|
|
|
|
```yaml
|
|
|
|
b:
|
|
|
|
c: cat
|
|
|
|
d:
|
|
|
|
- new thing
|
|
|
|
- foo thing
|
|
|
|
- bar thing
|
|
|
|
```
|
|
|
|
|
2018-02-27 21:07:34 +00:00
|
|
|
Note that the path is in quotes to avoid the square brackets 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
|
|
|
|
b.e[0].name: Howdy Partner
|
|
|
|
```
|
|
|
|
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
|
2017-05-02 22:11:26 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Values starting with a hyphen (or dash)
|
2017-05-03 00:42:53 +00:00
|
|
|
The flag terminator needs to be used to stop the app from attempting to parse the subsequent arguments as flags:
|
2017-05-02 22:11:26 +00:00
|
|
|
|
|
|
|
```
|
2017-12-17 22:11:08 +00:00
|
|
|
yq w -- my.path -3
|
2017-05-02 22:11:26 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
my:
|
2017-05-03 00:42:53 +00:00
|
|
|
path: -3
|
2017-05-02 22:11:26 +00:00
|
|
|
```
|
2017-09-26 02:36:49 +00:00
|
|
|
|
|
|
|
{!snippets/keys_with_dots.md!}
|