dos2unix

This page describes a problem with moving Python Programs from your Windows computer to a Linux computer, such as Gentry. The problem is the extra invisible "carriage return" character that Windows uses in text files. The problem is solved easily with the dos2unix command on Gentry.

The problem

Put this file on Gentry (or other Linux computer): fromwindows.zip

Gentry has the utility to unzip it:

unzip fromwindows.zip

and out pops fromwindows.py. Then,

cat fromwindows.py

reveals:

#this was made using Notepad in Windows
print "Good morning starshine, I'm from Windows!"

You can probably figure out what the program is intended to do. You probably think you know what you need to do to make the program executable. But you might find that the s.o.b. thing just doesn't work. On Gentry you will get an error:

: No such file or directory

Huh? Which file or directory is there "no such"? Weird.

The problem is that in DOS/windows files, lines are ended with both a carriage return and a linefeed (python's "\r" and "\n", ascii \015 and \012) while unix uses just a linefeed character. If you do

cat -ve fromwindows.py

you will see all the "\r" rendered with a ^M, and all the "\n" with a $:

#this was made using Notepad in Windows^M$
print "Good morning starshine, I'm from Windows!"^M$

The solution

dos2unix fromwindows.py
cat -ve fromwindows.py

The pesky ^M should be gone and the "killer app" fromwindows.py will be ready to go.


For quick inspection of bigger files, use:

cat -ve fromwindows.py | head

That will pipe the output of cat into head and show only the first 10 lines.


If you edit fromwindows.py with gvim or vim (which most of you probably do not) you will find a message [dos] at the bottom of the gvim window. This is also seen in the set summary resulting from entering the vim command set. You will see: fileformat=dos. To easily convert the file in your editor: set ff=unix.

Know this vim command also: set list. See Dealing with Makefile and other SOB files.

This is also interesting for vim users, at least to to see the pain and suffering: Tip #26: Getting rid of ^M - mixing dos and unix


Link(s)