How to insert newline character after comma in `),(` with sed?

sed does not support the \n escape sequence in its substitution command, however, it does support a real newline character if you escape it (because sed commands should only use a single line, and the escape is here to tell sed that you really want a newline character):

$ sed 's/),(/),\\
(/g' temp.txt
(foo),
(bar)
(foobar),
(foofoobar)

You can also use a shell variable to store the newline character.

$ NL='
'
$ sed "s/),(/,\\$NL(/g" temp.txt
(foo),
(bar)
(foobar),
(foofoobar)

Tested on Mac OS X Lion, using bash as shell.


You just have to escape with a backslash character and press the enter key while typing:

$ sed 's/),(/),\
(/g' temp.txt
(foo),
(bar)
(foobar),
(foofoobar)

This works for me:

$ echo "(foo),(bar)" | sed s/')','('/')',\\n'('/g
(foo),
(bar)

I am using:

  • GNU sed 4.2.2
  • GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)

Tags:

Regex

Sed