Submitted by: Phillip Lewis
The format of Windows and Unix text files differs slightly. In Windows, lines end with both the line feed and carriage return ASCII characters, but Unix uses only a line feed. As a consequence, some Windows applications will not show the line breaks in Unix-format files. Likewise, Unix programs may display the carriage returns in Windows text files with Ctrl-m ( ^M ) characters at the end of each line.
There are many ways to solve this problem. This document provides instructions for using FTP, unix2dos and dos2unix, tr, awk, perl, and vi to do the conversion.
Note: In the instructions below, replace unixfile.txt with the name of the Unix file, and replace winfile.txt with the name of the Windows file.
FTP
When using an FTP program to move a text file between Unix and Windows, be sure the file is transferred in ASCII format. This will ensure that the document is transformed into a text format appropriate for the host. Some FTP programs, especially graphical applications like Hummingbird FTP, do this automatically. If you are using FTP from the command line, however, before beginning the file transfer, be sure to enter at the FTP prompt:
ftp> ascii
dos2unix and unix2dos
On Unix / Linux systems, the utilities dos2unix and unix2dos are available. These utilities provide a straightforward method for converting files from the Unix command line.
To use either command, simply type the command followed by the name of the file you wish to convert, and the name of a file which will contain the converted results. Thus, to convert a Windows file to a Unix file, at the Unix prompt, enter:
$ dos2unix winfile.txt unixfile.txt
To convert a Unix file to Windows, enter:
$ unix2dos unixfile.txt winfile.txt
Note: On HP-UX machines these utilities are named dos2ux and ux2dos
tr
One can use tr to remove all carriage returns and Ctrl-z ( ^Z ) characters from a Windows file by entering:
$ tr -d '\15\32' < winfile.txt > unixfile.txt
One cannot use tr to convert a document from Unix format to Windows.
awk
To use awk to convert a Windows file to Unix, at the Unix prompt, enter:
$ awk '{ sub("\r$", ""); print }' winfile.txt > unixfile.txt
To convert a Unix file to Windows using awk, at the command line, enter:
$ awk 'sub("$", "\r")' unixfile.txt > winfile.txt
On some systems, the version of awk may be old and not include the function sub. If so, try the same command, but with gawk or nawk replacing awk.
perl
To convert a Windows text file to a Unix text file using perl, at the Unix shell prompt, enter:
$ perl -p -e 's/\r$//' < winfile.txt > unixfile.txt
To convert from a Unix text file to a Windows text file with perl, at the Unix shell prompt, enter:
$ perl -p -e 's/\n/\r\n/' < unixfile.txt > winfile.txt
One must use single quotation marks in either command line. This prevents the shell from trying to evaluate anything inside.
vi
In vi, you can remove the carriage return ( ^M ) characters with the following command:
:1,$s/^M//g
Note: To input the ^M character, press Ctrl-v , then press Enter or return.