gamechanger17
gamechanger17

Reputation: 4599

How to replace string with double quotes using sed

I have a string in my yaml file

prod:1.2.3"

In my bash

OLD_VERSION=1.2.3
NEW_VERSION=1.2.4

but I don't want to use

sed -i "" "s|$OLD_VERSION|$NEW_VERSION|g" test.txt

because there might be a chance that there would be 1.2.3 in some other place.So what I want to do instead is

replace 1.2.3" with 1.2.4"

how to add " in sed command

Upvotes: 2

Views: 1285

Answers (1)

anubhava
anubhava

Reputation: 785691

You may use this sed:

sed "s/:${OLD_VERSION//./\\.}\"/:${NEW_VERSION//./\\.}\"/g" file.yml

prod:1.2.4"

For the given values of the 2 variables it will execute this command:

sed 's/:1\.2\.3"/:1\.2\.4"/g' file.yml
  • ${OLD_VERSION//./\\.}: Replaces each dot with \. as dot matches any character
  • : before this string and " after this will make sure that we match exact string :1.2.3" instead of 11.2.3 or 1.2.33

Upvotes: 4

Related Questions