Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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>
</message>

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

<group ref="StandardMessageTrailer" required="true"/>

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>
</message>

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

like image 395
user301200 Avatar asked Mar 25 '10 00:03

user301200


People also ask

How do I add content from one file to another in Linux?

You can use cat with redirection to append a file to another file. You do this by using the append redirection symbol, ``>>''. To append one file to the end of another, type cat, the file you want to append, then >>, then the file you want to append to, and press <Enter>.

How to create multiline file with echo?

To add multiple lines to a file with echo, use the -e option and separate each line with \n. When you use the -e option, it tells echo to evaluate backslash characters such as \n for new line. If you cat the file, you will realize that each entry is added on a new line immediately after the existing content.


2 Answers

f2="$(<file2)"
awk -vf2="$f2" '/StandardMessageTrailer/{print f2;print;next}1' file1 

if you want sed, here's one way

sed  -e '/StandardMessageTrailer/r file2' -e 'x;$G' file1
like image 67
ghostdog74 Avatar answered Oct 15 '22 10:10

ghostdog74


If you can bear to make two passes, you can use a marker:

sed '/Standard/i MARKER' file1.xml | sed -e '/MARKER/r file2.xml' -e '/MARKER/d'

The trouble with trying to do it in one pass is that there's no way (that I know of) other than 'r' to insert the contents of a file, and 'r' does so in the output stream, out of reach of manipulation, after sed is finished with the line. So if the 'Standard' is in the last line, whatever you do with it will be over by the time file2 appears.

like image 34
Beta Avatar answered Oct 15 '22 11:10

Beta