I am attempting to use sed
to replace a regular expression capture group. I've read that this requires enabling extended regular expressions with the -E
flag. However, the following command is still not updating the text as expected.
echo "master-abcdef" | sed -i '' -E "s/IMAGE_TAG:\s*(\S )$/\1/g" values.yaml
Where values.yaml
has contents of:
global:
env: default
default:
IMAGE_TAG: dev-0be3323.zgm9a
... (more text below)
I am expecting values.yaml
to be replaced to:
global:
env: default
default:
IMAGE_TAG: master-abcdef
... (more text below)
CodePudding user response:
You may use this in any version sed
:
sed -i.bak -E 's/(IMAGE_TAG:[[:blank:]] )[^[:blank:]] /\1master-abcdef/' file.yml
cat file.yml
global:
env: default
default:
IMAGE_TAG: master-abcdef
... (more text below)
Here [[:blank:]]
matches a space or tab.
If you are using gnu-sed
then use:
sed -i -E 's/(IMAGE_TAG:\s )\S /\1master-abcdef/' file.yml
CodePudding user response:
With yq, you can write
yq eval '.default.IMAGE_TAG = "master-abcdef"' values.yaml
CodePudding user response:
You can use
repl="master-abcdef"
sed -i '' -E "s/(IMAGE_TAG:[[:space:]]*).*/\\1$repl/" values.yaml
Here,
(IMAGE_TAG:[[:space:]]*)
- captures into\1
anIMAGE_TAG:
string and then any zero or more whitespaces.*
- matches the rest of the string (here, line).
The \1$repl
replacement puts the captured value the repl
value in place of the matched text.