yq 工具

yq命令

用法:
  yq [标志]
  yq [命令]

示例:

# 如果未指定命令,yq 默认为 'eval' 命令。更多示例请参阅 "yq eval --help"
yq '.stuff' < myfile.yml    # 从 "myfile.yml" 中输出位于 "stuff" 节点的数据

yq -i '.stuff = "foo"' myfile.yml    # 就地更新 myfile.yml


可用命令:
  completion       生成指定shell的自动补全脚本
  eval             (默认)按顺序对每个 YAML 文件中的每份文档应用表达式
  eval-all         加载所有 YAML 文件的所有 YAML 文档,然后运行一次表达式
  help             任何命令的帮助

标志:
  -C, --colors                        强制使用颜色打印
  -e, --exit-status                   如果没有匹配项或返回 nullfalse,则设置退出状态
  -f, --front-matter string           (extract|process) 第一个输入作为 YAML 元数据。Extract 将提取 YAML 内容,process 将针对 YAML 内容运行表达式,保留其余数据不变
      --header-preprocess             吸收头部注释和分隔符,然后处理表达式。默认为真。
  -h, --help                          yq 帮助
  -I, --indent int                    设置输出缩进级别(默认为2-i, --inplace                       更新第一个给定文件的位置。
  -p, --input-format string           [yaml|y|xml|x] 输入解析格式。请注意,JSON 是 YAML 的子集。(默认为“yaml”)
  -M, --no-colors                     强制不带颜色打印
  -N, --no-doc                        不打印文档分隔符 (---)
  -n, --null-input                    不读取输入,只需评估给定的表达式。这对于从头创建文档很有用。
  -o, --output-format string          [yaml|y|json|j|props|p|xml|x] 输出类型。 (默认为“yaml”)
  -P, --prettyPrint                   美观打印,是 "... style ="" 的简写
  -s, --split-exp string              对每个结果(或文档)打印到名为(exp)的文件中。[exp] 参数必须返回字符串。您可以在表达式中使用 $index 作为结果计数器。
      --unwrapScalar                  解包标量,无引号、颜色或注释打印值(默认为真)
  -v, --verbose                       显示详细信息模式
  -V, --version                       打印版本信息并退出
      --xml-attribute-prefix string   XML 属性的前缀 (默认为"+")
      --xml-content-name string       当没有属性名称时,XML 内容的名称。 (默认为"+content")

使用 "yq [命令] --help" 了解有关命令的更多信息。

快速

# 4.18.1之前
cat file.yaml | yq e '.cool' -

# 4.18+
cat file.yaml | yq '.cool'
# 读取一个值:
yq '.a.b[0].c' file.yaml

# 来自 STDIN 的管道:
cat file.yaml | yq '.a.b[0].c'

# 就地更新 yaml 文件
yq -i '.a.b[0].c = "cool"' file.yaml

# 使用环境变量更新
NAME=mike yq -i '.a.b[0].c = strenv(NAME)' file.yaml

# 合并多个文件
yq ea '. as $item ireduce ({}; . * $item )' path/to/*.yml

# 对 yaml 文件进行多次更新
yq -i '
  .a.b[0].c = "cool" |
  .x.y.z = "foobar" |
  .person.name = strenv(NAME)
' file.yaml

在这里插入图片描述
在这里插入图片描述

Recipes

查找数组中的项目

- name: Foo
  numBuckets: 0
- name: Bar
  numBuckets: 0

yq '.[] | select(.name == "Foo")' sample.yml

name: Foo
numBuckets: 0

.[]展开数组,并将所有项目放入上下文中。
然后将这些项目通过管道 ( |)输入select(.name == "Foo"),将选择 name 属性设置为“Foo”的所有节点。

查找并更新数组中的项目

- name: Foo
  numBuckets: 0
- name: Bar
  numBuckets: 0

yq '(.[] | select(.name == "Foo") | .numBuckets) |= . + 1' sample.yml

- name: Foo
  numBuckets: 1
- name: Bar
  numBuckets: 0

从上面的示例开始,.[]splats 数组,选择过滤项目。

然后我们将|其通过管道 () 传送到.numBuckets,这将从所有匹配项中选择该字段

Splat、select 和字段都在括号中,整个表达式|=作为左侧表达式传递给运算符,作为. + 1右侧表达式传递给运算符。

|=是相对于其自身值更新字段的运算符,以点 ( ) 引用.。

该表达式. + 1会递增 numBuckets 计数器。

深度修剪一棵树

假设我们只对 child1 和 child2 感兴趣,并且想过滤掉其他所有内容。

parentA:
  - bob
parentB:
  child1: i am child1
  child3: hiya
parentC:
  childX: cool
  child2: me child2

yq '(
  .. |                                     # recurse through all the nodes
  select(has("child1") or has("child2")) | # match parents that have either child1 or child2
  (.child1, .child2) |                     # select those children
  select(.)                                # filter out nulls
) as $i ireduce({};                        # using that set of nodes, create a new result map
  setpath($i | path; $i)                   # and put in each node, using its original path
)' sample.yml

parentB:
  child1: i am child1
parentC:
  child2: me child2


查找所有匹配的child1和child2节点

使用 ireduce,仅使用这些节点创建一个新映射

使用其原始路径将每个节点设置到新地图中

对数组中的项目进行多次或复杂的更新

myArray:
  - name: Foo
    type: cat
  - name: Bar
    type: dog

yq 'with(.myArray[]; .name = .name + " - " + .type)' sample.yml

myArray:
  - name: Foo - cat
    type: cat
  - name: Bar - dog
    type: dog


with 运算符将有效地循环第一个给定表达式中的每个给定项目,并对其运行第二个表达式。

.myArray[]将数组展开到myArray.因此with将针对该数组中的每个项目运行

.name = .name + " - " + .type该表达式针对每个项目运行,将名称更新为原始名称和类型的串联。

按字段对数组进行排序

myArray:
  - name: Foo
    numBuckets: 1
  - name: Bar
    numBuckets: 0

yq '.myArray |= sort_by(.numBuckets)' sample.yml

myArray:
  - name: Bar
    numBuckets: 0
  - name: Foo
    numBuckets: 1

我们想去排序.myArray。

sort_by其工作原理是通过管道将一个数组输入其中,然后通过管道输出一个已排序的数组。

所以,我们使用|=更新.myArray。这与做相同.myArray = (.myArray | sort_by(.numBuckets))

Operators

Omit

Works likepick, but instead you specify the keys/indices that youdon’twant included.

Omit keys from map

myMap:
  cat: meow
  dog: bark
  thing: hamster
  hamster: squeak

yq '.myMap |= omit(["hamster", "cat", "goat"])' sample.yml

myMap:
  dog: bark
  thing: hamster

Omit indices from array

Note that non existent indices are skipped.

- cat
- leopard
- lion

yq 'omit([2, 0, 734, -5])' sample.yml

- leopard

Delete

Delete entry in map

# Given a sample.yml file of:
a: cat
b: dog

# then
yq 'del(.b)' sample.yml

# will output
a: cat

Delete nested entry in map

a:
  a1: fred
  a2: frood

yq 'del(.a.a1)' sample.yml

a:
  a2: frood

Delete entry in array

- 1
- 2
- 3

yq 'del(.[1])' sample.yml

- 1
- 3

Delete nested entry in array

- a: cat
  b: dog

yq 'del(.[0].a)' sample.yml

- b: dog

Delete no matches

a: cat
b: dog

yq 'del(.c)' sample.yml

a: cat
b: dog

Delete matching entries

a: cat
b: dog
c: bat

yq 'del( .[] | select(. == "*at") )' sample.yml

b: dog

Recursively delete matching keys

a:
  name: frog
  b:
    name: blog
    age: 12

yq 'del(.. | select(has("name")).name)' sample.yml

a:
  b:
    age: 12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值