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
You can replace newline (\n) with * character or word 'FOO':
sed '{:q;N;s/\n/*/g;t q}' /path/to/data.txt
OR
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
To update file use -i option:
sed -i '{:q;N;s/\n/\t/g;t q}' /path/to/data.txt
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 2 comments… read them below or add one }
thanks..
very nice example
It doesnt work for me :(
$ sed ‘{:q;N;s/\n/\t/g;t q}’ /fi/fa/foo.fu
sed: 1: “{:q;N;s/\n/\t/g;t q}”: unexpected EOF (pending }’s)