I have the following in a file (values.yaml):
global:
repo1:
enabled: true
repo2:
enabled: true
repo1:
replicaCount: 1
image:
tag: latest
pullPolicy: Always
repo2:
replicaCount: 1
image:
tag: latest
pullPolicy: Always
and I want to replace the tag: latest for repo1 with something like tag: newest1 and tag: latest for repo2 with something like tag: newest2 so that I end up with:
global:
repo1:
enabled: true
repo2:
enabled: true
repo1:
replicaCount: 1
image:
tag: newest1
pullPolicy: Always
repo2:
replicaCount: 1
image:
tag: newest2
pullPolicy: Always
So I'm trying to search a range from repo1:\n replicaCount:1 to either pullPolicy or the end of the file so I get a range that has just the one tag in it and can replace it.
I have
sed -i "" "N;/repo1:\n replicaCount:/,/pullPolicy/s/tag:.*/tag: newest1/g" values.yaml
and that almost works, but it always deletes the very last pullPolicy line like:
global:
repo1:
enabled: true
repo2:
enabled: true
repo1:
replicaCount: 1
image:
tag: newest1
pullPolicy: Always
repo2:
replicaCount: 1
image:
tag: newest2
And I'm on a Mac, which is why all the double quotes.
How do I specify the end range to either be the end of the file (I've also tried $ to no avail) or to be the pullPolicy?
sedwould be a poor choice of tool for doing this. See mikefarah.gitbook.io/yq for how to install/use a yaml editor and if you can't installyqfor some reason then you'd useawk.Nin your sed code to$!N. The lonely N is causing the last line to not be printed. Take care to backslash it since you are inside double quotes. For a robust way lookat my solution using python. I like your approach of combining N with range.