Yann Sagon
Yann Sagon

Reputation: 577

Filter entries using yq and regexp

I have the following sample and I want to get all the entries matching cpu* and gpu* (cpu042 and gpu001 in this case). When this works I want as well to filter the entries returned depending on their created and deleted date.

Sample:

nodes:
  cpu042:
    created: 20230123
    hardware:
      mem: 503
      cpu: 128
  node001:
    created: 20120123
    deleted: 20220120
    hardware:
      mem: 503
      cpu: 128
  gpu001:
    created: 20150123
    hardware:
      mem: 503
      cpu: 128

This what I tried:

yq_linux_amd64 '.nodes | select( .nodes | test("^(cpu|gpu)\\d+$"))' sample.sls

and this produces no output. I'm using Mikefarah's yq.

Upvotes: 2

Views: 962

Answers (1)

pmf
pmf

Reputation: 36033

Please include the exact output you expected.

Until then: You can test for the key using key:

yq '.nodes[] | select(key == "cpu*" or key == "gpu*")' sample.sls
created: 20230123
hardware:
  mem: 503
  cpu: 128
created: 20150123
hardware:
  mem: 503
  cpu: 128

Upvotes: 2

Related Questions