MAC OS X, Linux, Windows and other IT Tips and Tricks

11 Jun 13 Converting Windows text files to Linux

Here is a small overview of how (in)compatible computer operating systems are.
Normal text files end the lines as follows:
Windows: CR+LF
Linux: LF

In order to convert line ends here are few tricks I extracted from this page(many thanks to the author):

dos2unix and unix2dos

The utilities dos2unix and unix2dos are available for converting files from the Unix command line.
In Debian install the package dos2unix (apt-get install dos2unix)
The package comes with the following tools:

To convert a Windows file to a Unix file, enter:
dos2unix winfile.txt unixfile.txt
To convert a Unix file to Windows, enter:
unix2dos unixfile.txt winfile.txt


You can use tr to remove all carriage returns and Ctrl-z ( ^Z ) characters from a Windows file:
tr -d '\15\32' < winfile.txt > unixfile.txt
However, you cannot use tr to convert a document from Unix format to Windows.


To use awk to convert a Windows file to Unix, enter:
awk '{ sub("\r$", ""); print }' winfile.txt > unixfile.txt
To convert a Unix file to Windows, enter:
awk 'sub("$", "\r")' unixfile.txt > winfile.txt
Older versions of awk do not include the sub function.
In such cases, use the same command, but replace awk with gawk or nawk.


To convert a Windows text file to a Unix text file using Perl, enter:
perl -p -e 's/\r$//' < winfile.txt > unixfile.txt
To convert from a Unix text file to a Windows text file, enter:
perl -p -e 's/\n/\r\n/' < unixfile.txt > winfile.txt
You must use single quotation marks in either command line. This prevents your shell from trying to evaluate anything inside.


In vi, you can remove carriage return ( ^M ) characters with the following command:
Note: To input the ^M character, press Ctrl-v , and then press Enter or return.

In vim, to convert to Unix; use :
set ff=unix
to convert to Windows, use:
set ff=dos

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: