How to insert a newline in front of a pattern?

0 votes
asked Apr 6, 2009 by dennis

Not how to insert a newline before a line. This is asking how to insert a newline before a pattern within a line.

For example,

sed 's/regexp/&\n/g'

will insert a newline behind the regexp pattern.

How can I do the same but in front of the pattern?

Here is an example input file

somevariable (012)345-6789

Should become

somevariable
(012)345-6789

17 Answers

0 votes
answered Jan 6, 2009 by steve-b

in sed you can reference groups in your pattern with "\1", "\2", .... so if the pattern you're looking for is "PATTERN", and you want to insert "BEFORE" in front of it, you can use, sans escaping

sed 's/(PATTERN)/BEFORE\1/g'

i.e.

  sed 's/\(PATTERN\)/BEFORE\1/g'
0 votes
answered Apr 6, 2009 by tgamblin

In sed, you can't add newlines in the output stream easily. You need to use a continuation line, which is awkward, but it works:

$ sed 's/regexp/\
&/'

Example:

$ echo foo | sed 's/.*/\
&/'

foo

See here for details. If you want something slightly less awkward you could try using perl -pe with match groups instead of sed:

$ echo foo | perl -pe 's/(.*)/\n$1/'

foo

$1 refers to the first matched group in the regular expression, where groups are in parentheses.

0 votes
answered Apr 6, 2009 by dennis

Some of the other answers didn't work for my version of sed. Switching the position of & and \n did work.

sed 's/regexp/\n&/g' 

Edit: This doesn't seem to work on OS X, unless you install gnu-sed.

0 votes
answered Jan 13, 2011 by tmow
sed -e 's/regexp/\0\n/g'

\0 is the null, so your expression is replaced with null (nothing) and then...
\n is the new line

On some flavors of Unix doesn't work, but I think it's the solution at your problem.

echo "Hello" | sed -e 's/Hello/\0\ntmow/g'
Hello
tmow
0 votes
answered Apr 27, 2011 by roar-skullestad

On my mac, the following inserts a single 'n' instead of newline:

sed 's/regexp/\n&/g'

This replaces with newline:

sed "s/regexp/\\`echo -e '\n\r'`/g"
0 votes
answered Jan 1, 2012 by robert-casey

In vi on Red Hat, I was able to insert carriage returns using just the \r character. I believe this internally executes 'ex' instead of 'sed', but it's similar, and vi can be another way to do bulk edits such as code patches. For example. I am surrounding a search term with an if statement that insists on carriage returns after the braces:

:.,$s/\(my_function(.*)\)/if(!skip_option){\r\t\1\r\t}/

Note that I also had it insert some tabs to make things align better.

0 votes
answered Jan 14, 2012 by karthik

To insert a newline to output stream on Linux, I used:

sed -i "s/def/abc\\\ndef/" file1

Where file1 was:

def

Before the sed in-place replacement, and:

abc
def

After the sed in-place replacement. Please note the use of \\\n. If the patterns have a " inside it, escape using \".

0 votes
answered Apr 1, 2012 by vivek
echo one,two,three | sed 's/,/\
/g'
0 votes
answered Apr 20, 2012 by user1612632

In this case, I do not use sed. I use tr.

cat Somefile |tr ',' '\012' 

This takes the comma and replaces it with the carriage return.

0 votes
answered Apr 22, 2012 by mojuba

This works in bash, tested on Linux and OS X:

sed 's/regexp/\'$'\n/g'

In general, for $ followed by a string literal in single quotes bash performs C-style backslash substitution, e.g. $'\t' is translated to a literal tab. Plus, sed wants your newline literal to be escaped with a backslash, hence the \ before $. And finally, the dollar sign itself shouldn't be quoted so that it's interpreted by the shell, therefore we close the quote before the $ and then open it again.

Edit: As suggested in the comments by @mklement0, this works as well:

sed $'s/regexp/\\\n/g'

What happens here is: the entire sed command is now a C-style string, which means the backslash that sed requires to be placed before the new line literal should now be escaped with another backslash. Though more readable, in this case you won't be able to do shell string substitutions (without making it ugly again.)

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...