I am trying to change one specific word in yaml file using sed command
kind: Pod
metadata:
name: abc12
spec:
containers:
- image: ubuntu:latest
name: Ubuntu
imagePullPolicy: IfNotPresent
- image: ngnix: latest
name: Nginx
imagePullPolicy: IfNotPresent
...
Tried using the following command
sed '/^ * image: ngnix:latest/,/^*[^:]*:s/imagePullPolicy: IfNotPresent/imagePullPolicy: {}/' file.yaml
I am required to change the imagePullPolicy content only for ngnix using sed/awk command.
Following is my desired output:
kind: Pod
metadata:
name: abc12
spec:
containers:
- image: ubuntu:latest
name: Ubuntu
imagePullPolicy: IfNotPresent
- image: ngnix: latest
name: Nginx
imagePullPolicy: {}
...
Thanks
4
Answers
one case (GNU sed)
However, this may not be generic.
Using any POSIX awk:
The above assumes that
image:
always comes beforeimagePullPolicy:
as in the example you posted.Using GNU
sed
Fisrt, fix your YAML line 9:
image: ngnix: latest
=>image: nginx:latest
(2 errors).Then, use a proper parser,
yq
:Online Demo
Credits to emanuele6 from [email protected]
You need a little bit more work to include all needed objects.
To edit
inplace
, you can use-yi
.