I am trying to replate a pattern (that contains many specials chars) with a simple string that I have in a file called "file". I''m using sed to get it done. The command runs without any exception but the target file remains the same...
I'm trying to replace this:
<%=ENV["SECRET_KEY_BASE"]%>
with this (the content in the file):
88ce3e2c0fa0b667567dc370f8c62019ee88f1628ee1586651d7ea4a0faf18d0dbccaeb3f21c46109d2087ff31f07cf3734ce7a38f64f5a92cdf91dcb3494d1f** ... in a file called **secrets.yml
That's the commands I've tried but doesn't seem to work:
sed -i 's/<%=ENV["SECRET_KEY_BASE"]%>/$(cat file)/g' config/secrets.yml
also tried with variables this way, but nothing changed:
var1="<%=ENV["SECRET_KEY_BASE"]%>" && var2="`cat file`" && sed -i 's/$var1/$var2/g' config/secrets.yml
Does anyone have an idea what i'm missing here?
CodePudding user response:
There are two issues I see with what you have currently:
's/<%=ENV["SECRET_KEY_BASE"]%>/$(cat file)/g'
is using single quotes which will prevent interpretation of the command tocat
the file- You need to escape a few things in the comparison string like:
<%=ENV\[\"SECRET_KEY_BASE\"\]%>
Putting those things together, this is working for me:
sed -i "s/<%=ENV\[\"SECRET_KEY_BASE\"\]%>/$(cat file)/g" secrets.yml
CodePudding user response:
Certain special characters must be escaped when using sed. In this case it's the square brackets and the double quotes in the string to be replaced.
sed -i "s|<%=ENV\[\"SECRET_KEY_BASE\"\]%>|88ce3e2c0fa0b667567dc370f8c62019ee88f1628ee1586651d7ea4a0faf18d0dbccaeb3f21c46109d2087ff31f07cf3734ce7a38f64f5a92cdf91dcb3494d1f|g" secrets.yml