Search code examples
yq

filter keys which has a value containing "testing" and update using yq


I have the below code which I'm looking to find the keys containing testing and update the value of CloudProvider to x using yq. Please help me.

List1:
  - List2:
      - team: dba
        CloudProvider: aws
        Comment: testing
      - team: it
        CloudProvider: aws
        Comment: staging
      - team: qa
        CloudProvider: azure
        Comment: testing
      - team: qa
        CloudProvider: azure
        Comment: testing

Solution

  • Just traverse to the desired location, and wrap the traversal into parentheses to make it one entity for the assignment, so the overall structure won't change because the context isn't altered outside the parens:

    (.List1[].List2[] | select(.Comment == "testing")).CloudProvider = "x"
    
    List1:
      - List2:
          - team: dba
            CloudProvider: x
            Comment: testing
          - team: it
            CloudProvider: aws
            Comment: staging
          - team: qa
            CloudProvider: x
            Comment: testing
          - team: qa
            CloudProvider: x
            Comment: testing
    

    Works with kislyuk/yq and mikefarah/yq alike.