I have many k8s deployment files with yaml format.
I need to get the value of the first occurrence of name option only inside these files.
Example of deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
I need to get only the value nginx-deployment
I tried, but no success:
grep 'name: ' ./deployment.yaml | sed -n -e 's/name: /1/p'
3
Answers
With
sed
. Find first row withname
, remove everything before:
and:
and then quitsed
.Output:
If you have
yq
installed(similar tojq
) for yaml, this is a yaml aware tool and perhaps a robust tool for parsing yaml.Using awk :
With GNU
grep
(1) that supports the-P
flag.