Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a new-line replacement in a BSD sed?

Tags:

Greetings, how do I perform the following in BSD sed?

sed 's/ /\n/g' 

From the man-page it states that \n will be treated literally within a replacement string, how do I avoid this behavior? Is there an alternate?

I'm using Mac OS Snow Leopard, I may install fink to get GNU sed.

like image 694
Brett Ryan Avatar asked Sep 14 '09 13:09

Brett Ryan


People also ask

How do you use a new line character in sed?

By default, every line ends with \n when creating a file. The `sed` command can easily split on \n and replace the newline with any character. Another delimiter can be used in place of \n, but only when GNU sed is used. When the \n is missing in the last line of the file, GNU sed can avoid printing \n.


2 Answers

In a shell, you can do:

    sed 's/ /\ /g' 

hitting the enter key after the backslash to insert a newline.

like image 90
Wooble Avatar answered Oct 31 '22 20:10

Wooble


Another way:

sed -e 's/ /\'$'\n/g' 

See here.

like image 40
sikmir Avatar answered Oct 31 '22 19:10

sikmir