-
-
Notifications
You must be signed in to change notification settings - Fork 603
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
Comments
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) |
Thanks, I might take a stab at it. |
You could also pipe the output into a Ex:
|
i had to do this to remove the : at last. |
Expanding on @sumanthkumarc's one-liner:
This deals with several edge-cases, such as:
|
What would be the Like in
Suggestion from @mikefarah to use 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 ( yq.v2 r -j a.yml f | jq '.|keys[]' both outputs:
add yq.v2 r -j a.yml f | jq -r '.|keys[]' |
Workaround staying with GNU sed:
yaml_keys ()
{
yq r "$1" "$2" | sed -n -e '/^\([^ ]\([^:]\+\)\?\):/ s/:.*// p'
} Use with the same previous comment input: yaml_keys a.yml f outputs
|
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 |
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 |
Anyway to do it in v4? |
Got it... simple!
|
To get just keys in a bash-completion compatible way, I had to use parentheses: yq e '(.[] | keys)[]' test.yaml This converts --- # A sequence of keys.
- "key1": "value1"
- "key2": "value2" to
|
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? |
Is it currently possible to have the CLI only list the top level keys? For example, given the following yaml file:
Is there a way to only list
b
,d
ande
and no other subkeys with something likeyaml r test.yml a
?The text was updated successfully, but these errors were encountered: