How do I get the value of name
tag under metadata
section of below yaml file.
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
I need to get sss-pod-four
string.
grep "\sname: " config/pod.yaml | awk -F ": " '{print $2}'
The above code is printing
sss-pod-four
,
sss-test-container
and
sss-test-volume
Could you please try following and let me know if this helps you.
awk '/metadata/{flag=1} flag && /name:/{print $NF;flag=""}' Input_file
Adding a non one liner form of solution with explanation too now:
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.
You can also use this sed
command to reach your goal:
$ sed -n '/metadata:/,/spec:/p' input.yml | grep -oP '(?<=name: ).*'
sss-pod-four
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With