Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed append regex capture groups

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...

like image 702
Jahed Uddin Avatar asked Aug 23 '16 14:08

Jahed Uddin


1 Answers

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//
like image 138
anubhava Avatar answered Sep 28 '22 07:09

anubhava