I have some text like:

Blablabla <b>[intlink id="</b>2204<b>" type="page"] Blalala

I want replace it for remove the </b> and <b>, keeping the id="number". So it should result like:

Blablabla <b>[intlink id="2204" type="page"] Blalala

I try with:

sed -i 's@id="</b>[[:digit:]]\+<b>"@id="{1}"@g' ~/edit.txt
Blablabla <b>[intlink id="{1}" type="page"] Blalala

Also I try with:

sed -i 's@id="</b>[[:digit:]]\+<b>"@id="\\1"@g' ~/edit.txt

But I get:

Blablabla <b>[intlink id="\1" type="page"] Blalala

So, how I can keep the id number text in the regex digit?


\1 is the right syntax for referring to capture group 1; \\1 means you want the literal text \1. So change \\1 to \1, and then you need to put parentheses around the part you actually want to capture. This should work:

sed -i 's@id="</b>\([[:digit:]]\+\)<b>"@id="\1"@g' ~/edit.txt
  • It is not working with \1, it replaces to id="1" and not keeping the digit (I put the parenthesis too, not working), what could be the issue? I am running in bash – shakaran Sep 8 '15 at 5:04
  • @shakaran Te brackets were wrong set. The \(\) groupding includes now the whole number. – chaos Sep 8 '15 at 5:42
  • Ah yes, thanks. I accidentally left the + outside of the parentheses – Michael Mrozek Sep 8 '15 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.