Top Left Corner Welcome to Kim and Kevin's Home on the Web  
How do I convert between Unix and DOS text files?

The UNIX and DOS operating systems (which includes Microsoft Windows) differ in the format in which they store text files. DOS places both a line feed and a carriage return character at the end of each line of a text file, but Unix uses only a line feed character. Some DOS applications need to see carriage return characters at the ends of lines, and may treat Unix-format files as giant single lines. Some Unix applications won't recognize the carriage returns added by DOS, and will display Ctrl-m characters at the end of each line. This appears on the screen as ^M.

There are many ways to solve this problem. In this document we provide instructions on how to use FTP, screen capture, unix2dos and dos2unix, tr, awk, Perl, and Emacs, to do the conversion. Before you use these utilities, the files you are converting must first be on a Unix computer.
  • FTP

    When using an FTP program to move a text file between Unix and DOS, 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 Rapid Filer, do this automatically. If you are using ftp from the Unix or DOS prompt, however, before you begin the file transfer, be sure to enter at the ftp prompt:
      ascii
  • Screen Capture

    You can also convert files from Unix to DOS format when transferring them to a PC with a communications program by selecting ASCII text download. Select this option with your communications program to capture all the text subsequently displayed to your screen, and then enter at the Unix prompt:
      cat unixfile.txt
    Replace unixfile.txt with the name of the Unix text file you are transferring. Most communications programs will add carriage returns to the stream of text as they save it to your PC's hard drive. Once the file has finished displaying, abort the text download.

    Note that this method may be slow for large text files. Also, no error checking is performed on the file as it is transferred. Line noise may corrupt its contents, especially if you are using a terminal connect program such as HyperTerminal.

  • dos2unix and unix2dos

    On systems using SunOS, 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 the file you wish to convert, and the name of a file which will contain the converted results. Thus, to convert a DOS file to a Unix file, at the Unix prompt, enter:
      dos2unix dosfile.txt unixfile.txt
    To convert a Unix file to DOS, enter:
      unix2dos unixfile.txt dosfile.txt
    Note that these utilities are only available on SunOS systems (CDF runs SunOS).

  • tr

    You may use tr to remove all carriage returns and Ctrl-z (^Z) characters from a DOS file by entering
      tr -d '\15\32' < dosfile.txt > unixfile.txt

    You may use tr to remove all characters except octal 11, 12, 15, and 40-176, by entering
      tr -cd '\11\12\15\40-\176' < fileWithOddChars.txt > fileCleaned.txt
    tr cannot be used to convert a document from Unix format to DOS.

  • awk

    To use awk to convert a DOS file to Unix, at the Unix prompt, enter
      awk '{ sub("\r$", ""); print }' dosfile.txt > unixfile.txt
    To convert a Unix file to DOS using awk, at the command line, enter
      awk 'sub("$", "\r")' unixfile.txt > dosfile.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 DOS text file to a Unix text file using Perl, at the Unix shell prompt, enter:
      perl -p -e 's/\r$//' < dosfile.txt > unixfile.txt
    To convert from a Unix text file to a DOS text file with Perl, at the Unix shell prompt, enter:
      perl -p -e 's/$/\r/' < unixfile.txt > dosfile.txt
    Please note that you must use single quotation marks in either command line. This prevents your shell from trying to evaluate anything inside.

  • Emacs

    You can also convert a DOS file named dosfile.txt to a Unix text file using Emacs, a Unix text editor. Enter at the Unix shell prompt:
      emacs dosfile.txt
    This will open the file in the Emacs text editor. To remove all the "^M" characters, type
      M-% C-q C-m RET RET !
    It may also be necessary to remove a Ctrl-z at the end of the document. To quickly get to the end of the document in Emacs, type
      M->
    If you see a Ctrl-z at the end of the document, delete it.

    To convert a Unix file named unixfile.txt to a DOS text file, first open it in Emacs. At the Unix shell prompt, enter
      emacs unixfile.txt
    This will open the file in the Emacs text editor. To add carriage returns, which will show up as ^M in Emacs, type
      M-% C-q C-j RET C-q C-m C-q C-j RET !
    It may also be necessary to add a Ctrl-z at the end of the document. At the very end of the document, press
      C-q C-z
December 13, 2024
( Friday )
 
[TOP OF PAGE]
 



This site will look much better in a browser that supports web standards, but it is accessible to any browser or Internet device.