I am running an old version of git and I am writing a script that updates the host name configuration.
My problem is that I can’t figure out the right escape character combinations so that a variable is not mixed with the back-reference used in my regex.
Here is my best effort
#!/bin/bash
#code
foo=`ec2-describe-instances i-f95e4b82 | grep amazon | awk '{print $4}'`
sed -i "s/\(ec2-user@\)\(.*\)\(:repo\.git\)/\\1 $foo \\3/" /var/www/.git/config
This puts a space between the first back-reference and the variable foo. The hope is to not have that space between the first back reference and $foo. I suspect I could use a literal instead of the backreference, but the user names and repos may change.
Any ideas?
Edit
"s/\(ec2-user@\)\(.*\)\(:repo\.git\)/\\1$foo\\3/"
Does not produce the right answer because foo is amalgamated with the 1, producing the wrong back reference. I can’t figure out how to escape $foo.
You need to isolate the
$foovariable so you don’t need spaces around it to distingusih its name from the other surrounding characters. TryIf you want only a space after foo, then use
IHTH