92

I need to edit a good number of files, by inserting a line or multiple lines either right below a unique pattern or above it. Please advise on how to do that using sed, awk, perl (or anything else) in a shell. Thanks! Example:

some text
lorem ipsum dolor sit amet
more text

I want to insert consectetur adipiscing elit after lorem ipsum dolor sit amet, so the output file will look like:

some text
lorem ipsum dolor sit amet
consectetur adipiscing elit
more text
Sixtyfive
  • 1,150
  • 8
  • 19
vehomzzz
  • 42,832
  • 72
  • 186
  • 216
  • This was what showed up on Google much before the linked duplicate. Edited for grammar, style and example contents because of that. – Sixtyfive Jun 16 '19 at 14:30

4 Answers4

170

To append after the pattern: (-i is for in place replace). line1 and line2 are the lines you want to append(or prepend)

sed -i '/pattern/a \
line1 \
line2' inputfile

Output:

#cat inputfile
 pattern
 line1 line2 

To prepend the lines before:

sed -i '/pattern/i \
line1 \
line2' inputfile

Output:

#cat inputfile
 line1 line2 
 pattern
Kamal
  • 3,068
  • 5
  • 26
  • 26
  • 17
    The examples insert a space after `line1` (tested with GNU sed). The backslash should follow without a space: ``line1\``. Only the spaces after the `a` and `i` commands are ignored. – pabouk - Ukraine stay strong Dec 02 '15 at 22:11
  • 2
    how to mix the prepend while reading the lines from another file where `sed -i '/pattern/ r somefile.txt'`? – Aquarius Power May 01 '16 at 02:21
  • To avoid inserting space, just remove space between `i` (or `a`) character and `\`. – MarSoft Feb 23 '17 at 13:42
  • 1
    @AquariusPower In GNU sed, `/pattern/e cat somefile.txt` which will stick `somefile.txt` into the pattern space before `/pattern/`. This can be mroe useful that `r` which just sticks `somefile.txt` directly into the output stream. – stevesliva Oct 19 '17 at 13:56
  • to test it leave the i flag. – Timo Nov 02 '20 at 15:01
41

The following adds one line after SearchPattern.

sed -i '/SearchPattern/aNew Text' SomeFile.txt

It inserts New Text one line below each line that contains SearchPattern.

To add two lines, you can use a \ and enter a newline while typing New Text.

POSIX sed requires a \ and a newline after the a sed function. [1] Specifying the text to append without the newline is a GNU sed extension (as documented in the sed info page), so its usage is not as portable.

[1] https://unix.stackexchange.com/questions/52131/sed-on-osx-insert-at-a-certain-line/

Community
  • 1
  • 1
jahroy
  • 22,322
  • 9
  • 59
  • 108
6

Insert a new verse after the given verse in your stanza:

sed -i '/^lorem ipsum dolor sit amet$/ s:$:\nconsectetur adipiscing elit:' FILE
Sixtyfive
  • 1,150
  • 8
  • 19
alinsoar
  • 15,386
  • 4
  • 57
  • 74
  • 1
    Added an example. Note that 'Hello My love, you're dirty girl' must be the only string on the line. – alinsoar Jul 27 '12 at 20:46
  • @vehomzzz : The pattern below search the line containing the dirty girl. On that line, it execute the command substitute (s) replacing the end-of-line ($) by a return (\n) and the sexy knight line – Tinmarino Jun 30 '18 at 10:36
  • I've edited the question and all answers that refered to the questions' example. Just pointing it out so you can edit your comments accordingly if you wish to do so. – Sixtyfive Jun 16 '19 at 14:29
4

More portable to use ed; some systems don't support \n in sed

printf "/^lorem ipsum dolor sit amet/a\nconsectetur adipiscing elit\n.\nw\nq\n" |\
    /bin/ed $filename
Sixtyfive
  • 1,150
  • 8
  • 19
Chris Rees
  • 301
  • 2
  • 5
  • 1
    Note that you can add -*n* and +n commands, where *n* is any integer, to move up or down and therefore insert lines anywhere necessary in the region of the search string. – GKFX Jun 27 '20 at 09:59