1

I have a file called samconfig.toml

version = 0.1 ~~~ parameter_overrides = "CognitoUserPoolName=\"aaa\" ApiAllowOrigin=\"'https://dev.xxxxxxxxx.amplifyapp.com'\" etc.. ~~~ 

My sed command

sed -i '' -e "s#ApiAllowOrigin=\\\"'https.*com'\\\"#ApiAllowOrigin=\\\"'https://dev.yyyyyyyy.amplify.com'\\\"#g" ./samconfig.toml 

It doesn't substitute the url. What's confusing about this, is that it contains all those single quotes, double quotes, back slashes..

I even checked with visualized regexp checker

enter image description here

1
  • 2
    Enclosing sed commands within double quotes may have side effects it is safer to enclose those commands with single quotes. Single quotes can be represented by \' within a shell e.g. sed 'blah'\''blah'\''blah' file. Commented Aug 3, 2022 at 8:02

1 Answer 1

2

You may use this sed on macos:

sed -i.bak -E "s~(ApiAllowOrigin=\\\\\"'https://).*(\.com'\\\\\")~\1dev.yyyyyyyy.amplify\2~" file cat file version = 0.1 ~~~ parameter_overrides = "CognitoUserPoolName=\"aaa\" ApiAllowOrigin=\"'https://dev.yyyyyyyy.amplify.com'\" etc.. ~~~ 

Here:

  • When inside the double quotes you will need to use \\\\ to match a single \.
  • Note use or -E for extended regular expressions
  • We are using 2 capture groups to avoid repeating same pattern in match and substitution
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I'll try this later, thank you so much you've saved me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.