如何在 yaml 文件的 name 部分下获取 metadata 标签的值。

    apiVersion: v1
    kind: Pod
    metadata:
      name: sss-pod-four
      namespace: default
    spec:
      containers:
      - name: sss-test-container
        image: anaudiyal/infinite-loop
        volumeMounts:
        - mountPath: "/mnt/sss"
          name: sss-test-volume
      volumes:
        - name: sss-test-volume

我需要获取 sss-pod-four 字符串。
grep  "\sname: " config/pod.yaml | awk -F ": " '{print $2}'

上面的代码正在打印sss-pod-four ,sss-test-containersss-test-volume

最佳答案

您能否尝试关注并告诉我这是否对您有帮助。

awk '/metadata/{flag=1} flag && /name:/{print $NF;flag=""}'  Input_file

现在也添加带有解释的非单一线性形式的解决方案:
awk '
/metadata/{         ##checking a string metadata in current line here and if it is TRUE then do following:
 flag=1}            ##Making a variable named flag value TRUE here.
flag && /name:/{    ##Checking if variable named flag is TRUE here and current line has string name: in it then do following:
  print $NF;        ##Printing the last column of the current line here.
  flag=""           ##Making variable named flag value as NULL here.
}
' Input_file        ##Mentioning Input_file name here.

关于bash - shell 牌 : Get a specific word under a sub section in yaml file,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49190209/

10-10 17:51