Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've learned that the BSD version of sed included with OS X doesn't provide an intuitive way to add newlines. I've found a few notes about it, but I don't understand yet, how to extend the examples to solve my problem.

I need to append several lines to a file and found that the lines I add do not include newline characters.

My shell script currently contains the following:

sed '/match string/  a\
newline string' ./inputfile > ./outputfile

And, I'm trying to apply the tip I found here Newlines in sed on Mac OS X

share|improve this question
up vote 2 down vote accepted

Nevermind. While reformatting the code portion of this question, a solution became clear:

I found that the following does exactly what I need:

sed '/match string/  a\
newline string \
' ./inputfile > ./outputfile
share|improve this answer
To do that on a single line, you can use ANSI-C quoting to unescape \n. Additional backslashes are required before the newlines. echo $'111\n333' | sed $'/111/a\\\n222\\\n'. – user495470 Jun 9 '12 at 3:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .