Insert text before a certain line using Bash

前端 未结 5 1444
时光说笑
时光说笑 2020-12-17 04:24

How can I insert a set of lines (about 5) into a file at the first place a string is found?

For example:

BestAnimals.txt

dog         


        
5条回答
  •  旧时难觅i
    2020-12-17 04:49

    If you know (or somehow find out) the line:

    sed -n '/cat/=' BestAnimals.txt
    

    You can use sed:

    sed -i '2i giraffe' BestAnimals.txt
    

提交回复
热议问题