How do I remove newlines from a text file?

tr --delete '\n' < yourfile.txt
tr -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.

Leave a Comment