0

I've already looked at the following:

Escape a string for a sed replace pattern

Is it possible to escape regex metacharacters reliably with sed

They give absolutely no easy to understand answer.

version=1.2.3
sed -i -z -e 's/"version": "[A-Za-z0-9_.-]*"/"version": "$(version)"/' package.json

I'm trying to use a variable in a replace regular expression in a file. I don't have to use sed per say, as long as it works on macs and linux dists I'm ok with it.

basickarl
  • 37,187
  • 64
  • 214
  • 335
  • `sed -i -e 's/"version": "[^"]*"/"version": "'"$version"'"/' package.json` or `sed -i -e 's/\("version": "\)[^"]*/\1'"$version"'/' package.json` – Wiktor Stribiżew Feb 25 '19 at 12:38

1 Answers1

0

So I just had to use double quote for sed to recognise the 4version variable, then just escape the double quotes which are included otherwise.

sed -i -z -e "s/\"version\": \"[A-Za-z0-9_.-]*\"/\"version\": \"$version\"/" package.json
basickarl
  • 37,187
  • 64
  • 214
  • 335