68

Right now I'm using echo "Hello World" >> file.txt to append some text to a file but I also need to add text below a certain string let's say "[option]", is it possible with sed?

EG:

Input file

Some text
Random
[option]
Some stuff

Output file

Some text
Random
[option]
*inserted text*
Some stuff
  • 1
    You have to be more specific and give some sample text for people to help you with exact commands. – clement Mar 24 '14 at 14:51
  • please edit you Q and show the input and the output lines. Because you Q is unclear. You could also do echo "Hello World [option]" >> file.txt, but it doesn't make sense. – user55518 Mar 24 '14 at 14:56
  • I edited the question to provide more information for reference but the accepted answer was what I was trying to accomplish – Javier Villanueva Mar 24 '14 at 21:25
  • If you just want to edit a config file this is the best solution I found: unix.stackexchange.com/a/78076/20661 – rubo77 Jul 21 '16 at 8:20
102

Append line after match

  • sed '/\[option\]/a Hello World' input

Insert line before match

  • sed '/\[option\]/i Hello World' input

Additionally you can take backup and edit input file in-place using -i.bkp option to sed

  • 2
    on osx i get sed: 1: "/pattern/a some text here": command a expects \ followed by text – the_prole Jan 20 at 21:11
  • for Mac OSX, I needed to add \ <<NEWLINE>> after the a option – Tom Howard Aug 2 at 13:34
  • The above code will append/insert the line for every single match. If you want to append/insert the line for the first match only, you can prepend 0, to the commands: sed '0,/\[option\]/a Hello World' input or sed '0,/\[option\]/i Hello World' input – kimbaudi Aug 7 at 2:50
33

Yes, it is possible with sed:

sed '/pattern/a some text here' filename

An example:

$ cat test
foo
bar
option
baz
$ sed '/option/a insert text here' test
foo
bar
option
insert text here
baz
$
  • 2
    @javiervd I'm not sure what you meant by first answer? If you meant first by the time answered, then you're wrong. You didn't observe the timestamps carefully! – devnull Mar 25 '14 at 19:07
  • Is there a way to add it after the latest string matched? For example: foo bar option bar option baz . So the pattern become foo bar option bar option addedstring baz ? – KeepMove Dec 21 '15 at 12:45
4

With awk:

awk '1;/PATTERN/{ print "add one line"; print "\\and one more"}' infile

Keep in mind that some characters can not be included literally so one has to use escape sequences (they begin with a backslash) e.g. to print a literal backslash one has to write \\.

It's actually the same with sed but in addition each embedded newline in the text has to be preceded by a backslash:

sed '/PATTERN/a\
add one line\
\\and one more' infile

For more details on escape sequences consult the manual.

  • An awk line does not actually change the text, only output. – Josef Klimuk Jun 17 '18 at 14:51

protected by Community May 26 '17 at 9:21

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.