12

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:

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

The error that is reported is:

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

I've tried, using \n, \\ (as in the example), no character at all, just moving the second line to the next line. I've also tried something like:

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

EDIT!: Apologies, I wanted the text inserted BEFORE the existing, not after!

jww
  • 97,681
  • 90
  • 411
  • 885
MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106

9 Answers9

14

This should work:

sed -i '/Line to insert after/ i Line one to insert \
second new line to insert \
third new line to insert' file
anubhava
  • 761,203
  • 64
  • 569
  • 643
8

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
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
2

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 </plugins in pom.xml and inserts another XML object immediately preceding it, separated by a newline character.

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
Mark
  • 606
  • 7
  • 12
1

This ll works from the first line.. For eg: If you want to insert from 3rd line of a file, replace "1i" to "3i".

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

cat 1.txt

 line1
 line2
 line3
 Hai
vishnu raja
  • 21
  • 1
  • 7
1

When the lines to be inserted are the result of some command "mycmd" (like cat results.txt or printf "%s\n" line{1..3}), you can do

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

The last command can be simple modified when you want to insert before some match.

Walter A
  • 19,067
  • 2
  • 23
  • 43
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
Mahesh Kharvi
  • 389
  • 2
  • 6
0

This might work for you (GNU sed & Bash):

sed -i $'/Line to insert after/a\line1\\nline2\\nline3' file
potong
  • 55,640
  • 6
  • 51
  • 83
0

To be POSIX compliant and run in OS X, I used the following (single quoted line and empty line are for demonstration purposes):

sed -i "" "/[pattern]/i\\
line 1\\
line 2\\
\'line 3 with single quotes\`
\\
" <filename>
Andi
  • 3,249
  • 1
  • 20
  • 12
0

This can be easily done with Perl also

$ 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.
$
stack0114106
  • 8,534
  • 3
  • 13
  • 38