Search code examples
regexxmltextreplacegrep

Duplicate line and replace string


I have an XML file that contains more than 10,000 items. Each item contains a line like this.

<g:id><![CDATA[FBM00101816_BLACK-L]]></g:id>

For each item I need to add another line below like this:

<sku><![CDATA[FBM00101816]]></sku>

So I need to duplicate each g:id line, replace the g:id with sku and trim the value to delete all characters after the underscore (including it). The final result would be like this:

<g:id><![CDATA[FBM00101816_BLACK-L]]></g:id>
<sku><![CDATA[FBM00101816]]></sku>

Any ideas how to accomplish this?

Thanks in advance.


Solution

  • Okay, so after commenting, I couldn't help myself. This seemed to do what you asked for.

    find: <g:id><!\[CDATA\[([^\_]+)?(.+)?\]></g:id>
    replace: $0\n<sku><![CDATA[$1]></sku>
    

    I don't have BBEdit, but this is what it looked like in Textmate:

    enter image description here