How do I remove newlines from a text file? How do I remove newlines from a text file? shell shell

How do I remove newlines from a text file?


tr --delete '\n' < yourfile.txttr -d '\n' < yourfile.txt

Edit:

If none of the commands posted here are working, then you have something other than a newline separating your fields. Possibly you have DOS/Windows line endings in the file (although I would expect the Perl solutions to work even in that case)?

Try:

tr -d "\n\r" < yourfile.txt

If that doesn't work then you're going to have to inspect your file more closely (e.g. in a hex editor) to find out what characters are actually in there that you want to remove.


perl -p -i -e 's/\R//g;' filename

Must do the job.


tr -d '\n' < file.txt

Or

awk '{ printf "%s", $0 }' file.txt

Or

sed ':a;N;$!ba;s/\n//g' file.txt

This page here has a bunch of other methods to remove newlines.

edited to remove feline abuse :)