Thursday, March 24, 2016

Removing Newlines using tr

So at work today I was trying to strip a .txt file of all newlines.  I scoured the internet in search of a command, and either I'm terrible at Google (which is probably true) or there wasn't a direct answer that worked well for me.  After many attempts, I figured out a command that did exactly what I needed:


tr -d '\r\n' < myFile.txt | tee anotherFile.txt

This command strips the line feed (\n) and carriage return (\r) characters from myFile.txt and prints the result to the screen and anotherFile.txt.  You can replace '\r\n' with anything you wish to remove from the file.

Note that the text files were created on a Windows machine, hence the '\r\n'.  If this were created on a Unix/Linux machine, you only need '\n':



tr -d '\n' < myFile.txt | tee anotherFile.txt

Hopefully this helps you if other answers fail.