How do I remove newlines from a text file?

Alucard picture Alucard · Jun 28, 2010 · Viewed 278.3k times · Source

I have the following data, and I need to put it all into one line.

I have this:

22791

;

14336

;

22821

;

34653

;

21491

;

25522

;

33238

;

I need this:

22791;14336;22821;34653;21491;25522;33238;

EDIT

None of these commands is working perfectly.

Most of them let the data look like this:

22791

;14336

;22821

;34653

;21491

;25522

Answer

Tyler McHenry picture Tyler McHenry · Jun 28, 2010
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.