Difference between revisions of "yq parser"

From thelinuxwiki
Jump to: navigation, search
Line 21: Line 21:
  
 
  $ '''yq --arg IPADDR "$ipaddr" '.webserver.hosts[] | select(.ipaddr == "192.168.1.1")' foobar.yml'''
 
  $ '''yq --arg IPADDR "$ipaddr" '.webserver.hosts[] | select(.ipaddr == "192.168.1.1")' foobar.yml'''
 +
 +
show only keys
 +
$ '''yq '.webserver.hosts | keys | .[]' foobar.yml'''
 +
"bar"
 +
"foo"

Revision as of 15:37, 22 July 2026

file name foobarr.yml contents:

webserver:
  hosts:
    foo:
      ipaddr: 192.168.1.1
      desc: "foo"
    bar:
      ipaddr: 10.0.0.1
      desc: "bar"


$ 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

show only keys

$ yq '.webserver.hosts | keys | .[]' foobar.yml
"bar"
"foo"