Reputation: 331
If someone can please assist, I'm trying to do a sed append using regex and capture groups but its not working fully:
echo "#baseurl=http://mirror.centos.org/centos/$releasever/contrib/$basearch/" | sed -re '/#baseurl=http:\/\/mirror.centos.org(.*)/a baseurl=https:\/\/10.10.10.10\ \1'
#baseurl=http://mirror.centos.org/centos//contrib//
baseurl=https://10.10.10.10 1
At the moment it is just giving the literal value 1 rather than the capture group.
It should give:
#baseurl=http://mirror.centos.org/centos//contrib//
baseurl=https://10.10.10.10/centos//contrib//
I have tried backslash parentheses as well but its not working. Please assist....as it is hurting my head now...
Upvotes: 8
Views: 2678
Reputation: 785058
You can only capture back-reference when using s
(substitute) command in sed
.
This should working:
s="#baseurl=http://mirror.centos.org/centos/$releasever/contrib/$basearch/"
sed -r 's~#baseurl=http://mirror\.centos\.org(.*)~&\nbaseurl=https://10.10.10.10\1~' <<< "$s"
#baseurl=http://mirror.centos.org/centos//contrib//
baseurl=https://10.10.10.10/centos//contrib//
Upvotes: 9