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

How to insert newline after comma in ),( using sed?

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

Why is this not working?

+6
source share
2 answers

sed does not support the escape sequence \n in its wildcard command, however it does support the real newline if you avoid it (since sed should use only one line, and escape here to tell sed that you really want a newline ):

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

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

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

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

+6
source

You just need to escape with the backslash character and press the enter key as you type:

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

Source: https://habr.com/ru/post/909495/


All Articles