How to use newline replacement in BSD sed?

Hi, how do I do the following in BSD sed?

sed 's/ /\n/g' 

The man page says that \ n will be processed literally in the replacement string, how can I avoid this behavior? Is there an alternative?

I am using Mac OS Snow Leopard, I can install fink to get GNU sed.

+13
regex bsd sed
Sep 14 '09 at 13:13
source share
3 answers

In the shell you can:

  sed 's/ /\ /g' 

press enter after backslash to insert a new line.

+14
Sep 14 '09 at 13:18
source share
— -

Another way:

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

See here .

+6
Nov 09 '13 at 22:09
source share

For ease of use, I personally often use

 cr="\n" # or (depending version and OS) cr=" " sed "s/ /\\${cr}/g" 

therefore, it remains on line 1.

+4
Nov 11 '13 at 6:58
source share



All Articles