y
y
yq
Search…
Group By
This is used to group items in an array by an expression.

Group by field

Given a sample.yml file of:
1
- foo: 1
2
bar: 10
3
- foo: 3
4
bar: 100
5
- foo: 1
6
bar: 1
Copied!
then
1
yq eval 'group_by(.foo)' sample.yml
Copied!
will output
1
- - foo: 1
2
bar: 10
3
- foo: 1
4
bar: 1
5
- - foo: 3
6
bar: 100
Copied!

Group by field, with nuls

Given a sample.yml file of:
1
- cat: dog
2
- foo: 1
3
bar: 10
4
- foo: 3
5
bar: 100
6
- no: foo for you
7
- foo: 1
8
bar: 1
Copied!
then
1
yq eval 'group_by(.foo)' sample.yml
Copied!
will output
1
- - cat: dog
2
- no: foo for you
3
- - foo: 1
4
bar: 10
5
- foo: 1
6
bar: 1
7
- - foo: 3
8
bar: 100
Copied!
Last modified 29d ago