如果值是映射或字符串,则使用YQ测试

发布于 2025-02-12 05:34:55 字数 283 浏览 6 评论 0原文

我有包含两种表格的image键的YAML:

image: my/dockerimage:1.2.3

并且

image:
  description: this i a string
  type: another string

我需要处理此YAML并仅过滤键image是字符串(而不是映射)。

如何使用yq来执行此操作?

I have YAML that contains the image key with both forms:

image: my/dockerimage:1.2.3

and

image:
  description: this i a string
  type: another string

I need to process this YAML and filter only the keys where image is a string (not a map).

How can I do this with yq?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染火枫林 2025-02-19 05:34:55

假设 mikefarah/yq 已在此处使用(使用eval> evar> eval> evar> evary syntax) ,您可以通过将.image明确匹配为字符串类型来解决问题,该字符类型表示为!! str在yq中。 您需要以下需要获得.image的值

yq 'select(.image|type == "!!str").image'

因此, /#notice-for-v4.x-versions-prior-to-4.18.1“ rel =“ nofollow noreferrer”>上方4.18.1 ,eval Action e是默认值一个并且可以完全跳过。

Assuming that mikefarah/yq has been used here (with the eval syntax), you can solve the problem by explicitly matching the .image to be of string type, which is represented as !!str in yq. So you need below to get the value of .image

yq 'select(.image|type == "!!str").image'

Note that, if you are using yq version above 4.18.1, the eval action e is the default one and can be skipped altogether.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文