Issue
I have this file simple.yml
:
services:
- name: first
image: first_image
versionId: 1.0.0_first_image_id
- name: second
image: second_image
versionId: 2.0.1_second_image_id
I'm trying to replace the second occurrence of versionId
with this value: versionId: image_id_edited
so that the file would look like this:
services:
- name: first
image: first_image
versionId: 1.0.0_first_image_id
- name: second
image: second_image
versionId: image_id_edited
The value 2.0.1_second_image_id
here is changed often, so I cannot do a static search. Currently, I have this sed
command but it's not working:
sed -rz "s/versionId:\s*.*\$/versionId: image_id_edited/2" -i simple.yaml
I'd like to have a regex to search for the second versionId: ...
until it reaches the end of the line! Thanks!
Solution
You appear to be counting on the -z
flag to cause the whole input to be interpreted as a single line, so that the 2
flag to your s
command results in the second match in the whole file being substituted. The problem is that the relevant count is over non-overlapping matches. Your pattern being anchored to the end of the line, you need to match in multiline mode in order for there to be a possibility of two non-overlapping matches:
sed -rz 's/versionId:\s*.*$/versionId: image_id_edited/m2' -i simple.yaml
That is, add an m
flag to the s
command.
Answered By - John Bollinger Answer Checked By - Mary Flores (WPSolving Volunteer)