Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to list top level keys only? #20

Closed
jmreicha opened this issue Aug 23, 2017 · 13 comments
Closed

Any way to list top level keys only? #20

jmreicha opened this issue Aug 23, 2017 · 13 comments

Comments

@jmreicha
Copy link

Is it currently possible to have the CLI only list the top level keys? For example, given the following yaml file:

a:
  b:
    c:
    - 'test'
    - "test2"
  d:
  e:

Is there a way to only list b, d and e and no other subkeys with something like yaml r test.yml a?

@mikefarah
Copy link
Owner

Not at the moment sorry - happy to take a pull request for this.

Alternatively, you could pipe it out to json (-j), use jq then convert it back to yaml (yaml will take a json file and output yaml)

@jmreicha
Copy link
Author

Thanks, I might take a stab at it.

@simoniz0r
Copy link

You could also pipe the output into a grep -v to get rid of they subkeys you don't want to see. For example, test and test1 will both have multiple spaces before them in the output, so if you pipe yaml's output into a grep -v ' .*', this will output everything that does not start with two spaces.

Ex:

yaml r ./yamltest a
b:
  c:
  - test
  - test2
d: null
e: null
yaml r ./yamltest a | grep -v '  .*'
b:
d: null
e: null

@sumanthkumarc
Copy link

i had to do this to remove the : at last.
yq r test.yml my_key | grep -v ' .*' | sed 's/.$//'

@jlumbroso
Copy link

Expanding on @sumanthkumarc's one-liner:

yq r test.yml my_key | grep -v '^ .*' | sed 's/:.*$//'

This deals with several edge-cases, such as:

  • when the key's tree is empty;
  • there are spaces in the key name.

@Sylvain303
Copy link

What would be the yq syntax for extracting keys?

Like in jq?

yq r file.yml 'path.to.top_key|keys[]'

Suggestion from @mikefarah to use jq for that:

give this input

---
a:
b:
  c:
    - test
    - test2
d:
e:
f:
  key1: value1
  key2: value

filtering

yq.v2 r -j a.yml | jq '.f|keys[]'

Alternative filtering from yaml (jq like root key naming .f is not supported by yq)

yq.v2 r -j a.yml f | jq '.|keys[]'

both outputs:

"key1"
"key2"

add -r to jq for bash compatible extraction.

yq.v2 r -j a.yml f | jq -r '.|keys[]'

@Sylvain303
Copy link

Workaround staying with sed only solution, based on the fact that yq trim keys on left:

GNU sed:

  • -n no print
  • /^\([^ ]\([^:]\+\)\?\):/ match left anchored keys: non-space, followed optionally by non-colon
  • s/:.*/ remove colon plus end of the line
  • p print (behave like grep filtering)
yaml_keys () 
{ 
    yq r "$1" "$2" | sed -n -e '/^\([^ ]\([^:]\+\)\?\):/  s/:.*// p'
}

Use with the same previous comment input:

yaml_keys a.yml f

outputs

key1
key2

RyanSiu1995 added a commit to RyanSiu1995/yq that referenced this issue Jan 10, 2020
@mikefarah
Copy link
Owner

Just released a new version of yaml that fixes this - it's a pretty significant update so it's still in beta: https://github.com/mikefarah/yq/releases/tag/3.0.0-beta

mikefarah pushed a commit that referenced this issue Jan 12, 2020
@mikefarah
Copy link
Owner

Fixed in https://github.com/mikefarah/yq/releases/tag/3.0.1

check https://mikefarah.gitbook.io/yq/commands/read#path-only for more info

@eldada
Copy link

eldada commented Jan 28, 2021

Anyway to do it in v4?

@eldada
Copy link

eldada commented Jan 28, 2021

Anyway to do it in v4?

Got it... simple!

yq e '. | keys' example.yaml

@rsinnet
Copy link

rsinnet commented Oct 27, 2021

What would be the yq syntax for extracting keys?

Like in jq?

yq r file.yml 'path.to.top_key|keys[]'

Suggestion from @mikefarah to use jq for that:

give this input

---
a:
b:
  c:
    - test
    - test2
d:
e:
f:
  key1: value1
  key2: value

filtering

yq.v2 r -j a.yml | jq '.f|keys[]'

Alternative filtering from yaml (jq like root key naming .f is not supported by yq)

yq.v2 r -j a.yml f | jq '.|keys[]'

both outputs:

"key1"
"key2"

add -r to jq for bash compatible extraction.

yq.v2 r -j a.yml f | jq -r '.|keys[]'

To get just keys in a bash-completion compatible way, I had to use parentheses:

yq e '(.[] | keys)[]' test.yaml

This converts test.yaml from

--- # A sequence of keys.
- "key1": "value1"
- "key2": "value2"

to

key1
key2

@leonarac
Copy link

I'm working in bash, and I have a value stored in a variable obtained with yq, how can I use that variable in another yq query?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants