Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Linux SED RegEx replace, but keep wildcards

If I have a string that contains this somewhere (Foo could be anything):


How would I, using SED and RegEx, replace it with this:


My failed attempt:

echo "<tag>Foo</tag>" | sed "s/<tag>\(.*\)<\\/tag>/[tag]\1[\\/tag]"
like image 749
david Avatar asked Mar 21 '23 00:03


2 Answers

Your regex is missing the terminating /

$ echo "<tag>Foo</tag>" | sed "s/<tag>\(.*\)<\\/tag>/[tag]\1[\\/tag]/"
like image 51
grebneke Avatar answered Mar 23 '23 14:03


With this you can replace all types of tags and don't have to be tag specific.

$echo "<tag>Foo</tag>" | sed "s/[^<]*<\([^>]*\)>\([^<]*\)<\([^>]*\)>/[\1]\2[\3]/"

hope this helps.

like image 25
akkig Avatar answered Mar 23 '23 15:03