Insert multiple lines of text before specific line using Bash

后端 未结 9 1491
猫巷女王i
猫巷女王i 2020-12-16 03:01

I am trying to insert a few lines of text before a specific line, but keep getting sed errors when I try to add a new line character. My command looks like:



        
9条回答
  •  难免孤独
    2020-12-16 03:27

    For anything other than simple substitutions on individual lines, use awk instead of sed for simplicity, clarity, robustness, etc., etc.

    To insert before a line:

    awk '
    /Line to insert before/ {
        print "Line one to insert"
        print "second new line to insert"
        print "third new line to insert"
    }
    { print }
    ' /etc/directory/somefile.txt
    

    To insert after a line:

    awk '
    { print }
    /Line to insert after/ {
        print "Line one to insert"
        print "second new line to insert"
        print "third new line to insert"
    }
    ' /etc/directory/somefile.txt
    

提交回复
热议问题