How do I add a line of text to the middle of a file using bash?

前端 未结 6 990
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 22:28

I\'m trying to add a line of text to the middle of a text file in a bash script. Specifically I\'m trying add a nameserver to my /etc/resolv.conf file. As it stands, resol

6条回答
  •  遥遥无期
    2020-12-02 23:16

    This might work for you:

     sed -e '/nameserver/{x;/./b;x;h;i\nameserver 127.0.0.1' -e '}' resolv.conf
    

    Or GNU sed:

    sed -e '0,/nameserver/{//i\nameserver 127.0.0.1' -e '}' resolv.conf
    

提交回复
热议问题