Linux / UNIX: Sed Replace Newline

How do I replace newline (n) with sed under UNIX / Linux operating systems?

You can use the following sed command:

sed '{:q;N;s/n//g;t q}' /path/to/data.txt

sed ‘{:q;N;s/n//g;t q}’ /path/to/data.txt

You can replace newline (n) with * character or word ‘FOO’:

sed '{:q;N;s/n/*/g;t q}' /path/to/data.txt

sed ‘{:q;N;s/n/*/g;t q}’ /path/to/data.txt


sed '{:q;N;s/n/FOO/g;t q}' /path/to/data.txt

sed ‘{:q;N;s/n/FOO/g;t q}’ /path/to/data.txt

OR replace it with tab (t):

sed '{:q;N;s/n/t/g;t q}' /path/to/data.txt

sed ‘{:q;N;s/n/t/g;t q}’ /path/to/data.txt

To update file use -i option:

sed -i '{:q;N;s/n/t/g;t q}' /path/to/data.txt

sed -i ‘{:q;N;s/n/t/g;t q}’ /path/to/data.txt

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.