簡體   English   中英

yq v4:根據子項的鍵值刪除節點

[英]yq v4: delete node based on key-value of child

如何使用 yq v4 刪除具有“類型:數組”鍵/值對的所有(父)節點?

前:

info:
  title: My API
components:
  schemas:
    pets:
      type: array
      items:
        $ref: "#/components/schemas/pet"
    pet:
      type: object
      properties:
        petName:
          type: string

后:

info:
  title: My API
components:
  schemas:
    pet:
      type: object
      properties:
        petName:
          type: string

我用的是yq( https://github.com/mikefarah/yq/ )版本v4.30.8

我嘗試了很多 yq 命令,例如:

yq 'del(.components.schemas.[] | select(. == "array") | parent)' filename.yaml

, 但沒有成功。

如何使用“類型:數組”鍵/值對刪除所有(父)節點

要到達所有項目,請使用.. 向下遞歸文檔樹。 要過濾給定的鍵/值對,請在等式兩邊命名鍵和值。

yq 'del(.. | select(.type == "array"))' file.yaml

編輯: 如何只考慮.components.schemas下的節點?

如果您仍然想要遞歸,請在..前面加上.components.schemas | . 在沒有遞歸的情況下,將..替換為.components.schemas[]

yq 'del(.components.schemas | .. | select(.type == "array"))' file.yaml
yq 'del(.components.schemas[] | select(.type == "array"))' file.yaml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM