HowTo: Convert Between Unix and Windows text files

last updated in Categories ,

How can I convert newline [ line break or end-of-line (EOL) character ] between Unix and Windows text files?

A newline act as a end of line for all text files. It is a special character and the format of this character differs slightly under Windows and UNIX operating systems. The actual code for displaying a newline vary across oses as follows:


  1. Almost all Unix commands and text editor may display the EOL with Ctrl-m ( ^M ) characters at the end of each line for all text files created on MS-Windows operating systems.
  2. MS-Windows may not display line feed or EOL for all text files created on UNIX operating systems.

Option #1: dos2unix and unix2dos Commands

You can use the dos2unix and unix2dos as follows. To convert newline for a UNIX file to MS-Windows, type:
$ cat -v input.txt
$ unix2dos input.txt output.txt
$ cat -v output.txt
$ vi output.txt

To convert newline for a MS-Windows file to a Unix file, type:
$ cat -v input.txt
$ dos2unix input.txt output.txt
$ cat -v output.txt

Option #2: awk command

You can use the awk command to convert a MS-Windows file to Unix format, type:
$ cat -v input.txt
$ awk '{ sub("\r$", ""); print }' input.txt > output.txt
$ cat -v output.txt

You can convert newline for a Unix file to MS-Windows format, enter:
$ cat -v input.txt
$ awk 'sub("$", "\r")' input.txt > output.txt
$ cat -v output.txt

Please note that the cat command with the -v option is used to display all non-printing characters such as ^ and M- notation, except for LFD and TAB.

See also:


Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

4 comment

  1. This saved me a lot of effort.

     awk 'sub("$", "\r")' input.txt > output.txt

    it replaces line endigs in Linux, so that Windows Notepat can understand, where starts new line. Befor that command Notepad shows everything in one long line, however in Linux there were multiple lines.

  2. I’m using Ubuntu 12.04. For unix2dos and dos2unix version 5.3.1 (2011-08-09), you must use either of these two syntax (new file mode):

    # unix2dos -n infile outfile
    # unix2dos –newfile infile outfile

    If you use the syntax below then both a.txt and b.txt will be converted to DOS text files.  Both original files will be overwritten and you WILL LOSE the original files:

    # unix2dos a.txt b.txt

    1. 0

      down vote

      I have a text like “61403334343, password=clas2012” How can i make username and password in seperate lines? I am using like sed -e ‘s/password//g’ ‘s/username\n/g’` Please suggest

  3. Thanks a lot. this saved me a lot. using awk for converting newline for a Unix file to MS-Windows format

    Still, have a question? Get help on our forum!