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

时间:2015-08-14 09:48:32

标签: bash unix sed text-manipulation

我试图在特定行之前插入几行文本,但在尝试添加新行字符时不断出现sed错误。我的命令如下:

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 (如示例所示),根本没有字符,只是将第二行移动到下一行。我也尝试过这样的事情:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

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

8 个答案:

答案 0 :(得分:10)

这应该有效:

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

答案 1 :(得分:6)

除了单独行上的简单替换之外,为了简单,清晰,健壮等等,使用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

答案 2 :(得分:1)

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

如果要在匹配项之前插入,可以简单地修改最后一个命令。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

此ll从第一行起作用。例如:如果要从文件的第3行插入,请将“1i”替换为“3i”。

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

cat 1.txt

 line1
 line2
 line3
 Hai

答案 5 :(得分:0)

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

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

答案 6 :(得分:0)

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

答案 7 :(得分:0)

在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