【问题标题】:Insert multiple lines of text before specific line using Bash使用 Bash 在特定行之前插入多行文本
【发布时间】:2015-11-07 12:54:43
【问题描述】:

我正在尝试在特定行之前插入几行文本,但是当我尝试添加新行字符时不断收到 sed 错误。我的命令如下所示:

sed -r -i '/Line to insert after/ i Line one to insert \\
    second new line to insert \\
    third new line to insert' /etc/directory/somefile.txt

报错是:

sed: -e expression #1, char 77: unterminated `s' command

我试过了,使用\n\\(如示例),根本没有字符,只是将第二行移到下一行。我也尝试过类似的方法:

sed -r -i -e '/Line to insert after/ i Line one to insert'
    -e 'second new line to insert'
    -e 'third new line to insert' /etc/directory/somefile.txt

编辑!:抱歉,我希望在现有文本之前插入文本,而不是之后!

【问题讨论】:

    标签: bash unix sed text-manipulation


    【解决方案1】:

    这应该可行:

    sed -i '/Line to insert after/ i Line one to insert \
    second new line to insert \
    third new line to insert' file
    

    【讨论】:

    • 如果你在插入之后可能想使用a
    • 精湛的指南。非常感谢@anubhava
    • 有什么方法可以将写在单独文件中的多行插入到我们的文件中?类似this 的建议。
    【解决方案2】:

    对于单个行上的简单替换以外的任何内容,为了简单、清晰、稳健等原因,请使用 awk 而不是 sed。

    在一行之前插入:

    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
    

    在一行之后插入:

    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
    

    【讨论】:

      【解决方案3】:

      在 MacO 上,我需要更多的东西。

      • i 后的双反斜杠
      • -i 后面的空引号表示没有备份文件
      • 前导反斜杠添加前导空格
      • 尾随双反斜杠以添加换行符

      此代码在 pom.xml 中搜索</plugins 的第一个实例,并在其前面插入另一个 XML 对象,由换行符分隔。

      sed -i '' "/\<\/plugins/ i \\
      \            <plugin>\\
      \                <groupId>org.apache.maven.plugins</groupId>\\
      \                <artifactId>maven-source-plugin</artifactId>\\
      \                <executions>\\
      \                    <execution>\\
      \                        <id>attach-sources</id>\\
      \                        <goals>\\
      \                            <goal>jar</goal>\\
      \                        </goals>\\
      \                    </execution>\\
      \                </executions>\\
      \            </plugin>\\
      " pom.xml
      

      【讨论】:

      • 喜欢这个简洁的答案。谢谢。
      【解决方案4】:

      这将从第一行开始工作。例如:如果您想从文件的第 3 行插入,请将“1i”替换为“3i”。

      sed -i '1i line1'\\n'line2'\\n'line3' 1.txt 
      
      cat 1.txt
      
       line1
       line2
       line3
       Hai
      

      【讨论】:

        【解决方案5】:

        当要插入的行是某个命令“mycmd”的结果(如cat results.txtprintf "%s\n" line{1..3})时,您可以这样做

        sed -i 's/Line to insert after/r' <(cmd) file
        or 
        sed -i 's/Line to insert after/echo "&";cmd/e' file
        

        当您想在 一些匹配之前插入时,可以简单地修改最后一条命令。

        【讨论】:

          【解决方案6】:
          sed -i '/Line to insert after/ i\
          Line one to insert\
          second new line to insert\
          third new line to insert' /etc/directory/somefile.txt
          

          【讨论】:

          • 也许你应该解释一下你的改变是做什么的。
          【解决方案7】:

          这可能对你有用(GNU sed 和 Bash):

          sed -i $'/Line to insert after/a\line1\\nline2\\nline3' file
          

          【讨论】:

            【解决方案8】:

            为了符合 POSIX 并在 OS X 中运行,我使用了以下代码(单引号和空行用于演示目的):

            sed -i "" "/[pattern]/i\\
            line 1\\
            line 2\\
            \'line 3 with single quotes\`
            \\
            " <filename>
            

            【讨论】:

              【解决方案9】:

              这也可以用 Perl 轻松完成

              $ cat MeanwhileInHell.txt
              Iran|XXXXXX|Iranian
              Iraq|YYYYYY|Iraquian
              Saudi|ZZZZZ|Saudi is a Rich Country
              USA|AAAAAA|USA is United States of America.
              India|IIII|India got freedom from British.
              Scot|SSSSS|Canada Mexio.
              $ perl -pe 'BEGIN {$x="Line one to insert\nLine 2\nLine3\n"} $_=$x.$_ if /USA/ ' MeanwhileInHell.txt
              Iran|XXXXXX|Iranian
              Iraq|YYYYYY|Iraquian
              Saudi|ZZZZZ|Saudi is a Rich Country
              Line one to insert
              Line 2
              Line3
              USA|AAAAAA|USA is United States of America.
              India|IIII|India got freedom from British.
              Scot|SSSSS|Canada Mexio.
              $
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2016-06-16
                • 1970-01-01
                • 2014-07-04
                • 1970-01-01
                • 2013-06-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多