如何grep一个yaml值

Jan*_*Jan 2 shell grep yaml kubernetes

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

apiVersion: v1
items:
- apiVersion: v1
  kind: Pod
  spec:
    containers:
    hostIP: 12.198.110.192
    phase: Running
    podIP: 10.244.1.9
Run Code Online (Sandbox Code Playgroud)

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

    podIP: 10.244.1.9
Run Code Online (Sandbox Code Playgroud)

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

export PODIP=$(kubectl get pods -l run=hello-kube -o yaml | grep podIP)
Run Code Online (Sandbox Code Playgroud)

Cyr*_*rus 7

使用 awk:

kubectl get pods -l run=hello-kube -o yaml | awk '/podIP:/ {print $2}'
Run Code Online (Sandbox Code Playgroud)

输出:

10.244.1.9