Insert multiple lines of text before specific line using Bash

后端 未结 9 1466
猫巷女王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:21

    On MacOs I needed a few more things.

    • Double backslash after the i
    • Empty quotes after the -i to specify no backup file
    • Leading backslashes to add leading whitespace
    • Trailing double backslashes to add newlines

    This code searches for the first instance of in pom.xml and inserts another XML object immediately preceding it, separated by a newline character.

    sed -i '' "/\<\/plugins/ i \\
    \            \\
    \                org.apache.maven.plugins\\
    \                maven-source-plugin\\
    \                \\
    \                    \\
    \                        attach-sources\\
    \                        \\
    \                            jar\\
    \                        \\
    \                    \\
    \                \\
    \            \\
    " pom.xml
    

提交回复
热议问题