sed Insert a Newline Into the RHS of a Substitution

Posted on in Categories , , , , , last updated December 14, 2008

Q. How do I insert a newline into the RHS of a substitution using sed under Linux / UNIX like operating systems? My line pattern is as follows:

This is a test. This is another input.

I’d like to insert a newline after dot (.).

A. Simple use sed command as follows:

echo 'This is a test. This is another input.' | sed -e 's/\./&\n/'

You need to substitute dot and simply insert newline (\n) to replace with it.
If you are using older sed version try:

echo 'This is a test. This is another input.'  |sed 's/\./& \
> /'

Sample output:

This is a test. 
 This is another input.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Leave a Comment