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
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop










![sed Delete / Remove ^M Carriage Return [ Line Feed ]](http://s13.cyberciti.org/images/shared/rp/3/12.jpg)
![Linux / Unix: Sed Substitute Multiple Patterns [ Find & Replace ]](http://s13.cyberciti.org/images/shared/rp/3/0.jpg)

{ 4 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)
Doesn’t work for me in Cygwin, if there are 17 lines the first line has 16 of the replacement, line 2 will have 15 etc until the last line does not have the replacement.
Simpler way to replace the end of the line:
sed ‘s/$/FOO/’ /path/to/data.txt