HowTo: Convert Between Unix and Windows text files

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:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • Maris Dec 4, 2013 @ 8:15

    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.

  • bjorn.aigen Apr 27, 2014 @ 15:20

    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

    • Madhava Jun 20, 2016 @ 14:15


      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

  • vindhya Oct 7, 2015 @ 4:59

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

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum