Yq: How do I select multiple keys for output?

Created on 29 Oct 2019  路  9Comments  路  Source: mikefarah/yq

This seems like a basic question but I can't find examples anywhere in the documentation.
If I have input like

- alpha: 1
  beta: foo
  gamma: 0.3
  delta: bar
- alpha: 2
  beta: baz
  gamma: 1.23
  delta: buzz

And I want to select only the beta and delta keys, so that I get the following output:

- beta: foo
  delta: bar
- beta: baz
  delta: buzz

How do I do it? Every example in the documentation is only selecting a _single_ key.

The equivalent in jq would be something like:

jq '.[] | .beta, .delta'

or

jq '.[] | {beta, delta}'
enhancement

Most helpful comment

Would be great to document that this feature is missing. I wasted a lot of time trying to figure it out.

All 9 comments

This can't be done yet - would be neat though :)

Would be great to document that this feature is missing. I wasted a lot of time trying to figure it out.

hey, is this available now? i have a use case for exactly this, and wasted a lot of time to figure out how to do this

Yeah cool - sorry for your time wasted - I'd love to add this at some stage but for now I'll add it to the readme as a known missing feature

Super, i would like to take a stab at it

Cool - I've been thinking about it, there are other related issues about being able to:

  • use AND, OR operators
  • nest expressions (e.g. find by child).

And I think they all can be solved by refactoring the code that handles path expressions as an array into a proper path expression tree. This will allow multiple keys for output by an expression like a.(b.thing or c.other.thing or d.whatever) as well as a whole bunch of other combos.

Other commands (like write) use the same code, so they too will benefit by being able to give complex expressions to update..

I don't think it will be easy though...

https://www.geeksforgeeks.org/expression-tree/ for reference.

yq would need to parse the path string into a path expression tree, then data-navigator will need to be updated to recursively navigate via the expression tree (rather than the expression array it uses atm)

Might take it on myself actually - it's so fundamental to the whole codebase.

Sorry - normally I definitely welcome help, but this is a significant bit if work that affects the core

cool, no worries. Will wait for you to add these functionalities

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nicholasdille picture nicholasdille  路  7Comments

OnkelTem picture OnkelTem  路  8Comments

rgm3 picture rgm3  路  7Comments

il1yaz picture il1yaz  路  3Comments

dakky picture dakky  路  5Comments