2

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

gamechanger17
  • 4,025
  • 6
  • 22
  • 38
  • 1
    Does this answer your question? [How do I escape slashes and double and single quotes in sed?](https://stackoverflow.com/questions/7517632/how-do-i-escape-slashes-and-double-and-single-quotes-in-sed) – Wiktor Stribiżew Feb 17 '22 at 17:52
  • 1
    Incorrect dupe because OP is trying to use shell variables in `sed` that need escaping in shell before they are evaluated in shell. Presence of starting `:` and ending `"` is also specific to this problem – anubhava Feb 17 '22 at 19:46

1 Answers1

4

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
anubhava
  • 761,203
  • 64
  • 569
  • 643