开发者

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

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.xm开发者_开发技巧l  

but I want to insert it before.

appreciate the help


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


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.


Usually I do like this:

  1. file1, file to read insert content
  2. file2, insert reading content from file1 to at the head of file2
  3. script snippet:

    sed "\$r ${file2}" ${file1} > tmpfile
    mv tmpfile ${file2}


Here were the solutions that worked for me:

  1. Using a marker like explained in another reply:

    sed '/StandardMessageTrailer/i MARKER' file1.xml | sed -e '/MARKER/r file2.xml' -e '/MARKER/d'
    
  2. Counting the line when it occured like explained in another reply to a similar question:

    LINE_NUMBER_MATCHING=$(sed -n '/StandardMessageTrailer/=' file1.xml) && sed "$((${LINE_NUMBER_MATCHING} - 1))r file2.xml" file1.xml
    
  3. Or using sed like explained in another reply to a similar question:

    sed $'/StandardMessageTrailer/{e cat file2.xml\n}' file1.xml
    


I tried the different solutions and the one from Beta did the work for me.

Summary :

  • I wanted to insert different files into a main file
  • I wanted to use markers to say where I wanted these files to be inserted

Example :
Create 2 files :

cloud_config.yml:

coreos:
__ETCD2__

etcd2.yml :

  etcd2:
    name:                         __HOSTNAME__
    listen-peer-urls:             http://__IP_PUBLIC__:2380
    listen-client-urls:           http://__IP_PUBLIC__:2379,http://127.0.0.1:2379

Then we run that script on it :

sed '/Standard/i __ETCD2__' cloud_config.yml \
| sed -e "/__ETCD2__/r etcd2.yml" > tmpfile
sed "s|__ETCD2__||g" tmpfile > cloud_config.yml

Finally, we got that :

coreos:
  etcd2:
    name:                         __HOSTNAME__
    listen-peer-urls:             http://__IP_PUBLIC__:2380
    listen-client-urls:           http://__IP_PUBLIC__:2379,http://127.0.0.1:2379
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜