jq
and yq
for querying, filtering, and transforming YAML files
#107
grahamgower
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It sometimes happens that one wants to extract some specific bit of information from a YAML file, or modify it in some way. And reaching for a parser like demes-python can be unnecessary or awkward depending on the task at hand. The json querier
jq
and its YAML wrapperyq
are well suited for such tasks. The jq docs require some patient reading (maybe a couple of hours rather than a couple of minutes), so here are some examples to indicate possible use cases.Extracting a Demes block that is embedded in another yaml.
It has been proposed that we could/should embed the Demes demography into a more general simulator-specific YAML file. E.g. taking the YAML example from tskit-dev/msprime#1842 (comment)
Transforming a non-compliant Demes file into a compliant one
With demes-python 0.2.0, we modified pulses to use a list of sources and proportions. We can convert pre 0.2.0 yaml files to the new format with a short jq script (can be input in one line, but using a separate file is clearer).
Then, given an old yaml file ex2.yaml,
Rename demes
Suppose you want to rename the demes in your file(s)? No problem!
As input, let's pipe the output from convert.jq above.
Caveats
If we want to use a fully-resolved Demes file as the input to yq, then we find that yaml
.inf
values are converted to1.7976931348623157e+308
(jq converts infinity to the largest representable floating point number)But, this is no problem. We just transform those infinity values!
Beta Was this translation helpful? Give feedback.
All reactions