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

前端 未结 6 1002
伪装坚强ぢ
伪装坚强ぢ 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:05

    Assuming you want to insert immediately after the search line, this is much simpler:

    sed -ie '/^search/a nameserver 127.0.0.1' filename
    
    • -i : edit file in place
    • -e : allows the execution of a script/commands inside sed expression
    • a mynewtext : command that tells sed to insert the text mynewtext after matched pattern

提交回复
热议问题