3

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

  • Before or after? – ctrl-alt-delor Jun 7 '15 at 18:53
  • Are the blank lines actually part of your input file? Are all lines in that format or should some lines be ignored? – terdon Jun 7 '15 at 19:03
  • No, there are no blank lines, I just made those blank line for better overview, should have mentioned that. No lines should be ignored. – SH1986 Jun 7 '15 at 21:26
  • Where do you close that span tag? – glenn jackman Jun 8 '15 at 16:55
3

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>
  • 1
    Replace \{1,3\} with + to get sed 's/subtitle[0-9]+">/&<span style="style0">/' unless it is important for it to go wrong with numbers of more than 3 digits. + means one or more of the previous thing. – ctrl-alt-delor Jun 7 '15 at 18:59
  • I think + is ERE which isn't supported by POSIX sed – bkmoney Jun 7 '15 at 19:38
  • you are correct, you need to use \+. – ctrl-alt-delor Jun 7 '15 at 19:58
  • I found out that this sed 's/subtitle[0-9]\{1,3\}">/&<span style="style0">/' was the best one for me. – SH1986 Jun 7 '15 at 20:05
1

If all your lines are as you show, all you need to do is enter the new string after the first > on each line:

$ sed 's/>/><span style="style0">/' file 
<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>
  • I can see that this one will add <span style="style0"> after the <br/> also. – SH1986 Jun 7 '15 at 20:06
  • @SH1986 no, it only replaces the first > not all. You need the g flag to replace more. – terdon Jun 7 '15 at 22:06
  • Took the words right out of my mouth man. I had to look twice at it because I was trying to look for some occurrence shortcut at first, but it was very soon apparent that the problem was how to insert a second <...> after the first <...>. – mikeserv Jun 8 '15 at 0:45

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.