Difference between revisions of "yq parser"

From thelinuxwiki
Jump to: navigation, search
Line 1: Line 1:
file contents:
+
file name devices_test.yml contents:
 
<source lang="yaml">
 
<source lang="yaml">
 
webserver:
 
webserver:
Line 12: Line 12:
  
  
  $ '''yq '.fg.hosts[] | select(.ipaddr == "192.168.1.1")' devices_test.yaml'''
+
  $ '''yq '.webserver.hosts[] | select(.ipaddr == "192.168.1.1")' devices_test.yml'''
  
 
  {
 
  {
 
   "ipaddr": "192.168.1.1"
 
   "ipaddr": "192.168.1.1"
 
  }
 
  }
 +
 +
pass an arg
 +
 +
$ '''yq --arg IPADDR "$ipaddr" '.webserver.hosts[] | select(.ipaddr == "192.168.1.1")' devices_test.yml'''

Revision as of 21:35, 21 May 2025

file name devices_test.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")' devices_test.yml
{
 "ipaddr": "192.168.1.1"
}

pass an arg

$ yq --arg IPADDR "$ipaddr" '.webserver.hosts[] | select(.ipaddr == "192.168.1.1")' devices_test.yml