Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete an entry with the key matching regex using yq

I have a json like this:

{
  "sha256key": [
    "438143050f234146300c44ed7e22be698e1e3c8c6bbc89b5c7e6276e8250a3a7"
  ],
  "validkey": [
    "77989d8d29d044112026eae94fe27e9df9ae29d9987d38bcdf470100175def76",
    "3c91a35a4e12eb89c8f25381f29438f270e9b0af1ef0cbcd416581cd7fe06a0e"
  ],
}

I want to remove all entries which have the key started with sha256

Expected result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "validkey": [
    "77989d8d29d044112026eae94fe27e9df9ae29d9987d38bcdf470100175def76",
    "3c91a35a4e12eb89c8f25381f29438f270e9b0af1ef0cbcd416581cd7fe06a0e"
  ],
}

I have tried

yq 'del(select(.key | test("^sha256.")) this does nothing

I’m using go yq

>Solution :

mikefarah/yq allows you to use * for globbing:

yq -oj 'del(."sha256*")'

If you want to test by your regular expression, select by matching against the key:

yq -oj 'del(.[] | select(key | test("^sha256.")))'

Output:

{
  "validkey": [
    "77989d8d29d044112026eae94fe27e9df9ae29d9987d38bcdf470100175def76",
    "3c91a35a4e12eb89c8f25381f29438f270e9b0af1ef0cbcd416581cd7fe06a0e"
  ]
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading