Converting Line Endings

Posted on August 20, 2015 by Destry Hines

Text files on Windows vs. Unix computers have different ways of identifying the end of each line. On Windows computers there are two characters at the end of each line, a Carriage Return (CR) and a Line Feed (LF). On Linux or any Unix type OS, the end is identified by just a Line Feed (LF).

Sometimes you need to convert a text file from one type to the other. Below is one way to do it from the command line on most Unix OSs. 

To go from Windows -> Unix
(note: don't use the same file name or it will be empty)

tr -d '\r' < winfile.txt > unixfile.txt

To go from Unix -> Windows.

awk 'sub("$", "\r")' unixfile.txt > winfile.txt


Posted in Linux   Tagged carriage return, line feed, unix, windows