How to insert newline character after comma in ),(
with sed?
$ more temp.txt
(foo),(bar)
(foobar),(foofoobar)
$ sed 's/),(/),\n(/g' temp.txt
(foo),n(bar)
(foobar),n(foofoobar)
Why this doesn't work?
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.
OK, I know this question is old but I just had to wade trough this to make sed accept a \n character. I found a solution that works in bash and I am noting it here for others who run into the same problem.
To restate the problem: Get sed to accept the backslash escaped newline (or other backslash escaped characters for that matter).
The workaround in bash is to use:
In bash a $'\n' string is replaced with a real newline.
The only other thing you need to do is double escape the \n as you have to escape the slash itself.
To put it all together:
If you want it actually changed instead of being printed out use the -i