Yq: Document selection of elements by value

Created on 26 Aug 2018  路  7Comments  路  Source: mikefarah/yq

If there is a way to select an array element by value then please add it to the read examples.
Perhaps this ticket could be blocked by "add a way to select portions of the array by value."

For example, given:

outputs:
  - foo:
    enabled: false
  - bar:
    enabled: true
  - baz:
    cat: pretty

I'm hoping to understand how to select the 'baz' array element at index 2 to examine its interesting properties. I'm also hoping to understand how to select the "bar" element at index 1 by filtering for "enabled=true."

enhancement

Most helpful comment

I have a use case based on this but is somewhat different.

input

kind: Ingress
metadata:
  name: test
---
kind: Deployment
metadata
  name: foo
---
[...]
---
kind: Ingress
metadata:
  name: second

Now I would like to do

$ yq 'filter(.kind == "Ingress")'
kind: Ingress
metadata:
  name: test
---
kind: Ingress
metadata:
  name: second

All 7 comments

This would be a very cool feature, but unfortunately you cannot do that in yq yet.

Yep, filtering by value would be a major improvement. 馃挴

I have a use case based on this but is somewhat different.

input

kind: Ingress
metadata:
  name: test
---
kind: Deployment
metadata
  name: foo
---
[...]
---
kind: Ingress
metadata:
  name: second

Now I would like to do

$ yq 'filter(.kind == "Ingress")'
kind: Ingress
metadata:
  name: test
---
kind: Ingress
metadata:
  name: second

I would like to solve this issue, but go-modules seem not to work:

go test -cover
go: finding github.com/inconshreveable/mousetrap v1.0.0
go: finding github.com/pkg/errors v0.0.0-20180311214515-816c9085562c
go: finding github.com/spf13/pflag v0.0.0-20180601132542-3ebe029320b2
go: finding gopkg.in/imdario/mergo.v0 v0.3.5
go: finding gopkg.in/mikefarah/yaml.v2 v2.3.0
go: finding gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473
go: finding gopkg.in/spf13/cobra.v0 v0.0.3
go: gopkg.in/mikefarah/[email protected]: parsing go.mod: unexpected module path "gopkg.in/yaml.v2"
go: error loading module requirements

I have a use case based on this but is somewhat different.

input

kind: Ingress
metadata:
  name: test
---
kind: Deployment
metadata
  name: foo
---
[...]
---
kind: Ingress
metadata:
  name: second

Now I would like to do

$ yq 'filter(.kind == "Ingress")'
kind: Ingress
metadata:
  name: test
---
kind: Ingress
metadata:
  name: second

I have the same use case.
And I tried with the new feature (https://mikefarah.gitbook.io/yq/usage/path-expressions#search-by-children-nodes) but I'm not sure how it works when we have multiple documents.

I tried:

$ yq - -d'*' '.(kind == "Deployment")'

@mikefarah did you introduce go modules?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nicholasdille picture nicholasdille  路  7Comments

IdanAdar picture IdanAdar  路  9Comments

mkyc picture mkyc  路  6Comments

brunowego picture brunowego  路  5Comments

andrewsav-bt picture andrewsav-bt  路  6Comments