Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

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

$ more temp.txt
(foo),(bar)
(foobar),(foofoobar)

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

Why this doesn't work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

sed does not support the 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...