Reputation: 113
Insert attributes (strings) within existing tags.
I need to insert the number found within <pagenum><pagenum/>
into the id=
attribute so in this example.
I start with:
<pagenum page="normal" id="page">1<pagenum/>
and I need to replace it with:
<pagenum page="normal" id="page1">1<pagenum/>
The string in there could be any alphanumeric value.
What would this regex look like? Something like this?
/s/<pagenum page="normal" id="page">1<pagenum//>/<pagenum page="normal" id="page"{Value}>1\<pagenum/>
I`m too rusty on my backreferencing...
Upvotes: 1
Views: 297
Reputation: 56915
Try:
s!(<pagenum page="normal" id=")([a-z0-9_-]+)(">)([0-9]+)(<pagenum/>)!\1\2\4\3\4\5!i
e.g.:
echo '<pagenum page="normal" id="page">1<pagenum/>' | \
sed -r 's!(<pagenum page="normal" id=")([a-z0-9_-]+)(">)([0-9]+)(<pagenum/>)!\1\2\4\3\4\5!i'
Note - isn't a closing tag usually </pagenum>
as opposed to <pagenum/>
?
Upvotes: 2