使用 Bash 在特定行之前插入多行文本

Insert multiple lines of text before specific line using Bash

我试图在特定行之前插入几行文本,但是当我尝试添加新行字符时,总是出现 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

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

这应该有效:

sed -i '/Line to insert after/ i Line one to insert \
second new line to insert \
third new line to insert' file
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

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

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

除了单行上的简单替换之外,为了简单、清晰、健壮等原因,请使用 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 行插入,请将“1i”替换为“3i”。

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

cat 1.txt

 line1
 line2
 line3
 Hai

为了在 OS X 中兼容 POSIX 和 运行,我使用了以下内容(单引号行和空行用于演示目的):

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

当要插入的行是某些命令"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

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

这也可以用 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.
$

在 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