Converting between DOS and Unix


Quick Overview

DOS (and Windows) use different formats to mark the ned of a line in a text file. DOS uses a CR-LF, while Unix just uses a LF. There are many ways to switch between the formats.. some common ones are below

Explicit Programs

The programs dos2unix and unix2dos provide a simple way to switch between the two formats. These are also known on some systems as todos and fromdos.

tr

tr -d '\15\32' < dosfile > unixfile

AWK

DOS to Unix
awk '{ sub("\r$", ""); print }' < dosfile > unixfile
Unix to DOS
awk 'sub("$", "\r")' < unixfile > dosfile

Perl

DOS to Unix
perl -p -e 's/\r$//' < dosfile > unixfile
Unix to DOS
perl -p -e 's/\n/\r\n/' < unixfile > dosfile

Enjoy.


Leave a Reply

Your email address will not be published. Required fields are marked *