How can I can insert the contents of a file into another file right before a specific line

Posted by jelbatnigi on Stack Overflow See other posts from Stack Overflow or by jelbatnigi
Published on 2010-03-25T00:14:50Z Indexed on 2010/03/25 0:23 UTC
Read the original article Hit count: 561

Filed under:

Hi, How can I can insert the contents of a file into another file right before a specific line using sed.
example I have file1.xml that has the following:

   <field tagRef="376">
    </field>
    <field tagRef="377">
    </field>
    <field tagRef="58">
    </field>
    <group ref="StandardMessageTrailer" required="true"/>
</fieldList>

and file2.xml has the following:

    <field tagRef="9647">
        <description>Offset</description>
    </field>
    <field tagRef="9648">
        <description>Offset Units/Direction</description>
    </field>
    <field tagRef="9646">
        <description>Anchor Price</description>
    </field>

how can i insert the contents of file2 into file1 just before

so it will look like this:

   <field tagRef="376">
    </field>
    <field tagRef="377">
    </field>
    <field tagRef="58">
    </field>
    <field tagRef="9647">
        <description>Offset</description>
    </field>
    <field tagRef="9648">
        <description>Offset Units/Direction</description>
    </field>
    <field tagRef="9646">
        <description>Anchor Price</description>
    </field>
    <group ref="StandardMessageTrailer" required="true"/>
</fieldList>

I know how to insert after that line using

sed 'group ref="StandardMessageTrailer"/r file2.xml' file1.xml > newfile.xml

but I want to insert it before.

appreciate the help

© Stack Overflow or respective owner

Related posts about sed