Inserting into middle of a string using sed

sedtext processing

I'm trying to insert a string into multiple lines using sed, but i don't get the desired result

I have this:

<p begin="540960420t" end="600189590t" xml:id="subtitle8">-Some text.<br/>-Some more text</p>
<p begin="601020420t" end="653572920t" xml:id="subtitle9">-Something else<br/>Some more text</p>
<p begin="654403750t" end="731560830t" xml:id="subtitle10">More words<br/>-more text</p>
<p begin="732401670t" end="786625840t" xml:id="subtitle11">Some text.<br/>Some more text</p>

and I need to insert this

<span style="style0">

Before the "subtitle[0-200]"> so it will be like this:

<p begin="540960420t" end="600189590t" xml:id="subtitle8"><span style="style0">-Some text.<br/>-Some more text</p>
<p begin="601020420t" end="653572920t" xml:id="subtitle9"><span style="style0">-Some text.<br/>Some more text</p>

etc…

I was trying something like this:

cat demo.xml | sed 's/xml:id="subtitle[0-9]">/<span style="style0"><p/g'

But this replaces the xml:id="subtitle[0-9]"> and only from subtitle0 to subtile9

I am quite new to all this sed and regexp thing, if something else should be easier than sed then it's fine ..

Best Regards
Soren

Best Answer

You can do:

sed 's/subtitle[0-9]\{1,3\}">/&<span style="style0">/'

This matches the subtitle[0-200] by matching 1 to 3 digits using the \{1,3\} and replaces this with the matched pattern with the <span style... added.

If you input:

<p begin="540960420t" end="600189590t" xml:id="subtitle8">-Some text.<br/>-Some more text</p>

<p begin="601020420t" end="653572920t" xml:id="subtitle9">-Something else<br/>Some more text</p>

<p begin="654403750t" end="731560830t" xml:id="subtitle10">More words<br/>-more text</p>

<p begin="732401670t" end="786625840t" xml:id="subtitle11">Some text.<br/>Some more text</p>

This will output

<p begin="540960420t" end="600189590t" xml:id="subtitle8"><span style="style0">-Some text.<br/>-Some more text</p>

<p begin="601020420t" end="653572920t" xml:id="subtitle9"><span style="style0">-Something else<br/>Some more text</p>

<p begin="654403750t" end="731560830t" xml:id="subtitle10"><span style="style0">More words<br/>-more text</p>

<p begin="732401670t" end="786625840t" xml:id="subtitle11"><span style="style0">Some text.<br/>Some more text</p>
Related Question