首页 > 解决方案 > 如何grep一个yaml值

问题描述

我尝试从 shell 中的 YAML 文件中获取值:

apiVersion: v1
items:
- apiVersion: v1
  kind: Pod
  spec:
    containers:
    hostIP: 12.198.110.192
    phase: Running
    podIP: 10.244.1.9

我得到kubectl get pods -l run=hello-kube -o yaml | grep podIP:这个输出:

    podIP: 10.244.1.9

我的目标是将该值保存在环境变量中,但我只得到key/value-pair:

export PODIP=$(kubectl get pods -l run=hello-kube -o yaml | grep podIP)

标签: shellgrepkubernetesyaml

解决方案


使用 awk:

kubectl get pods -l run=hello-kube -o yaml | awk '/podIP:/ {print $2}'

输出:

10.244.1.9

推荐阅读