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

前端 未结 5 534
囚心锁ツ
囚心锁ツ 2020-12-05 07:46

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:



        
5条回答
  •  一生所求
    2020-12-05 08:13

    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
    

提交回复
热议问题