y
y
yq
Search…
Split into multiple files
yq can split out the results into multiple files with the --split-exp/s flag. You will need to give this flag an expression (that returns a string), this will be used as the filename for each result. In this expression, you can use $index to represent the result index in the name, if desired.

Split documents into files

Given a file like
1
a: test_doc1
2
---
3
a: test_doc2
Copied!
Then running:
1
yq e -s '.a' myfile.yml
Copied!
will result in two files:
test_doc1.yml:
1
a: test_doc1
Copied!
test_doc2.yml:
1
---
2
a: test_doc2
Copied!
TIP: if you don't want the leading document separators (---), then run with the --no-doc flag.

Split documents into files, using index

This is like the example above, but we'll use $index for the filename. Note that this variable is only defined for the --split-exp/s flag.
1
yq e -s '"file_" + $index' myfile.yml
Copied!
This will create two files, file_0.yml and file_1.yml.

Split single document into files

You can also split results into separate files. Notice
1
- name: bob
2
age: 23
3
- name: tim
4
age: 17
Copied!
Then, by splatting the array into individual results, we can split the content into several files:
1
yq e '.[]' file.yml -s '"user_" + .name'
Copied!
will result in two files:
user_bob.yml:
1
name: bob
2
age: 23
Copied!
user_tim.yml:
1
name: tim
2
age: 17
Copied!
Last modified 29d ago