Difference between revisions of "yq parser"
From thelinuxwiki
(Created page with "file contents: <source lang="yaml"> webserver: hosts: foo: ipaddr: 192.168.1.1 desc: "foo" bar: ipaddr: 10.0.0.1 desc: "bar" </source> $ '...") |
|||
| (10 intermediate revisions by one user not shown) | |||
| Line 1: | Line 1: | ||
| − | file contents: | + | file name foobarr.yml contents: |
<source lang="yaml"> | <source lang="yaml"> | ||
webserver: | webserver: | ||
| Line 5: | Line 5: | ||
foo: | foo: | ||
ipaddr: 192.168.1.1 | ipaddr: 192.168.1.1 | ||
| − | desc: "foo" | + | desc: "foo is cool" |
bar: | bar: | ||
ipaddr: 10.0.0.1 | ipaddr: 10.0.0.1 | ||
| − | desc: "bar" | + | desc: "bar is cooler" |
</source> | </source> | ||
| − | $ '''yq '. | + | $ '''yq '.webserver.hosts[] | select(.ipaddr == "192.168.1.1")' foobar.yml''' |
{ | { | ||
| − | "ipaddr": " | + | "ipaddr": "192.168.1.1" |
| + | }yq '.webserver.hosts[] | select(.ipaddr == "192.168.1.1")' foobar.yml | ||
| + | |||
| + | pass an arg | ||
| + | |||
| + | $ '''yq --arg IPADDR "$ipaddr" '.webserver.hosts[] | select(.ipaddr == "192.168.1.1")' foobar.yml''' | ||
| + | { | ||
| + | "ipaddr": "192.168.1.1", | ||
| + | "desc": "foo" | ||
} | } | ||
| + | |||
| + | show only keys | ||
| + | $ '''yq '.webserver.hosts | keys | .[]' foobar.yml''' | ||
| + | "bar" | ||
| + | "foo" | ||
| + | |||
| + | csv of key + ipaddr (without quotes), use the -r (--raw-output) option: | ||
| + | |||
| + | $ '''yq -r '.webserver.hosts | to_entries[] | "\(.key),\(.value.ipaddr)"' foobar.yml''' | ||
| + | foo,192.168.1.1 | ||
| + | bar,10.0.0.1 | ||
Latest revision as of 15:50, 23 July 2026
file name foobarr.yml contents:
webserver: hosts: foo: ipaddr: 192.168.1.1 desc: "foo is cool" bar: ipaddr: 10.0.0.1 desc: "bar is cooler"
$ yq '.webserver.hosts[] | select(.ipaddr == "192.168.1.1")' foobar.yml
{
"ipaddr": "192.168.1.1"
}yq '.webserver.hosts[] | select(.ipaddr == "192.168.1.1")' foobar.yml
pass an arg
$ yq --arg IPADDR "$ipaddr" '.webserver.hosts[] | select(.ipaddr == "192.168.1.1")' foobar.yml
{
"ipaddr": "192.168.1.1",
"desc": "foo"
}
show only keys
$ yq '.webserver.hosts | keys | .[]' foobar.yml "bar" "foo"
csv of key + ipaddr (without quotes), use the -r (--raw-output) option:
$ yq -r '.webserver.hosts | to_entries[] | "\(.key),\(.value.ipaddr)"' foobar.yml foo,192.168.1.1 bar,10.0.0.1