Insert text before a certain line using Bash

前端 未结 5 1441
时光说笑
时光说笑 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条回答
  •  温柔的废话
    2020-12-17 04:29

    awk -v insert=giraffe -v before=cat '
      $1 == before && ! inserted {
        print insert
        inserted++
      }
      {print}
    ' BestAnimals.txt > NewBestAnimals.txt
    

提交回复
热议问题