Convert text files from DOS to UNIX and vice versa.

Here are few recipes to convert DOS text files to UNIX.

How do you know whether you have a DOS text file or a UNIX text file? A line feed and a carriage return terminate the DOS text files lines. UNIX uses only a line feed character. By using the Unix command file you can figure out which type of file you are dealing with.
$ file dosfile.txt 
dosfile.txt: ASCII text, with CRLF line terminators
$ file unixfile.txt 
unixfile.txt: ASCII text

$ file dosfile.txt
dosfile.txt: ASCII text, with CRLF line terminators
$ file unixfile.txt
unixfile.txt: ASCII text

To convert files back and forth from one format to another you have several options. A few of them are showed here.

Using tr

DOS to UNIX only
The tr utility copies the standard input to the standard output with substitution or deletion of selected characters.
$ tr -d '\r' < dosfile.txt > unixfile.txt

$ tr -d '\r' < dosfile.txt > unixfile.txt

Using Vim

No need to introduce vim. Vim is an improved version of vi, and runs on almost all Unix systems as well as on Windows.
$ vim dosfile.txt
...
:set FileFormat=unix
:wq

$ vim dosfile.txt
...
:set FileFormat=unix
:wq

If you have a Unix text file to convert you can type :set FileFormat=dos. You can also replace FileFormat by the abbreviation FF. You can type :help FileFormat to have more information on this option.

Using Emacs

Emacs is the extensible, customizable, self-documenting real-time display editor.
In the status bar at the bottom of your screen Emacs shows information about the file you are editing. In the following exemple, the file is a DOS file.

http://fred.velvnet.com/emacs-dos.png

The function set-buffer-file-encoding-system will let you set the file coding-system of the current buffer to CODING-SYSTEM. This means that when you save the buffer, it will be converted according to CODING-SYSTEM. The following command will set the coding system for your buffer to UNIX. Next time you will save your file this encoding system will apply. Instead of Unix type dos to select the DOS encoding system.
M-x set-buffer-file-coding-system Unix

M-x set-buffer-file-coding-system Unix

http://fred.velvnet.com/emacs-dos.png

Using sed

There are several flavors of sed depending on the operating system you are using or which package is installed on your computer. The following methods should work with all the flavors of sed available.
As we said before, lines are terminated by CR/LF in the DOS file format. The following command suppresses the last two characters of the line, and then adds the default line termination character for UNIX which is LF.
$ sed 's/.$//' dosfile.txt  > new_unixfile.txt

$ sed 's/.$//' dosfile.txt > new_unixfile.txt

The conversion from UNIX file format to DOS is more complicated. In the old versions of sed you could not include special characters to the rule. Therefore, we need to call an external program from inside the substitution rule to add the character CR.
$ sed 's/$'"/`echo -e "\r"`/" unixfile.txt > new_dosfile.txt

$ sed 's/$'"/`echo -e "\r"`/" unixfile.txt > new_dosfile.txt

If by chance you are using a more recent version of sed or a sed extended such as gnu sed you can simply do this.
$ sed 's/$/\r/' unixfile.txt > new_dosfile.txt

$ sed 's/$/\r/' unixfile.txt > new_dosfile.txt

Using Perl

Perl is pretty straightforward, you add or remove the CR character at the end of the file.
$ perl -p -e 's/\r$//' < dosfile.txt > new_unixfile.txt
$ perl -p -e 's/$/\r/' < unixfile.txt > new_dosfile.txt

$ perl -p -e 's/\r$//' < dosfile.txt > new_unixfile.txt
$ perl -p -e 's/$/\r/' < unixfile.txt > new_dosfile.txt

Using awk

The same recipe using awk.
$ awk '{sub("\r$", "", $0);print $0}' dosfile.txt > new_unixfile.txt
$ awk '{sub("$", "\r", $0);print $0}' unixfile.txt > new_dosfile.txt

$ awk '{sub("\r$", "", $0);print $0}' dosfile.txt > new_unixfile.txt
$ awk '{sub("$", "\r", $0);print $0}' unixfile.txt > new_dosfile.txt

Using Python

This one is more for the fun. I really like Python, this is my favorite language but I rarely, not to say never do python one-liners. For me Python is not really adapted for writing one-liners.
$ python -c "import sys; map(sys.stdout.write, (l[:-2] + '\n' for l in sys.stdin.readlines()))" < dosfile.txt  > new_unixfile.txt

$ python -c "import sys; map(sys.stdout.write, (l[:-2] + '\n' for l in sys.stdin.readlines()))" < dosfile.txt > new_unixfile.txt

Conclusion

There are other ways to convert DOS file format to UNIX file format. Personally I am a heavy Emacs user, and when I have to convert text files, or programs files, I use Emacs. For huge data files, I prefer to use Perl. You don't want to open a file of several hundred of Giga Bytes in you Emacs or VI.
 

Comments

Posted by: film izle Dec 23, 2009 @ 17:01

thanks so nice geat article.

Posted by: flowers phoenix Dec 27, 2009 @ 01:52

this is great info!

Posted by: replica watches Jan 03, 2010 @ 21:34

We are Rolex watch experts selling only the finest, top level condition, and preowned men's and ladies' Rolex watches, used Rolex watches, unused Rolex watches and new Rolex watches on the market. Our top priority is to provide every customer with the highest quality Rolex watch at the lowest price available. Browse our wide selection of Rolexes for sale.

Posted by: fake rolex watches Jan 04, 2010 @ 23:34

Welcome to our website,and you will enjoy the sunshine service. Let's

abandon all the worry,and enjoy the happy shopping travel.All the

replica watches paypal and replica watches are most in fashion

boutique this year.rolex replica and rolex replica paypal and the

replica rolex paypal show your chaming temperament. In addtin, the

fake watches paypal and the other fake rolex paypal make you mold

your unique glamour this winter.The swiss rolex replica have the high

quality but low price. replica watch paypal and replica rolex have

all the style and cheapest price replica rolex watches. Enjoying the

fun of net purchase! replica rolex We will supply you the perfect

service and after sale support system. Our honest will bring you

absolute satisfaction.

Posted by: ugg boots Jan 07, 2010 @ 04:08

this is great info!

Posted by: Pes Patch Jan 07, 2010 @ 09:52

great information

Posted by: replica watches Jan 07, 2010 @ 22:01

The tag heuer rubber strap rolex doesn’t stray from rolex watches the strap pattern. Even replica watches though breitling watches the strap patek philippe patterns of audemars piguet watches from this brand chanel watches are familiar to us, it approaches a very sport and innovative montblanc watches appearance. This brand fake watches are always trying new replica watches watches with rolex watches new designs. This New replica rolex Concord C1 Chronograph tag heuer Watch can best embody this feature.This time Concord breitling watches offers us

Posted by: film izle Jan 17, 2010 @ 04:42

Thanks for your good website and for sharing your experiences.

Posted by: divx film izle Jan 17, 2010 @ 04:45

Thank you for sharing your friends. Hope to see you another day.

Posted by: links of london Jan 19, 2010 @ 00:21

This brand fake watches are always trying new replica watches watches with rolex watches new designs.

Posted by: Soulwhisper Jan 22, 2010 @ 00:11

great information http://www.airmax-shox.com/en

Posted by: Ankara halı yıkama Feb 07, 2010 @ 04:26

The tag heuer rubber strap rolex doesn’t stray from rolex watches the strap pattern. Even replica watches though breitling watches the strap patek philippe patterns of audemars piguet watches from this brand chanel watches are familiar to us, it approaches a very sport and innovative montblanc watches appearance

Posted by: ankara koltuk yıkama Feb 07, 2010 @ 04:27

boutique this year.rolex replica and rolex replica paypal and the replica rolex paypal show your chaming temperament. In addtin, the fake watches paypal and the other fake rolex paypal make you mold your unique glamour this winter.The swiss rolex replica have the high quality but low price.

Posted by: evening dresses Feb 19, 2010 @ 06:10

I will be sure to look around more

Posted by: Nike Air Max Feb 22, 2010 @ 22:55

Very creative, one of the nicer sites I have seen today. Keep up the great work.

Posted by: Nike Air Max Mar 02, 2010 @ 23:51

So beautiful sharing!Thank you very much.

Posted by: Nike Air Max Mar 11, 2010 @ 18:52

Best wishes!Your blog is very good!

Posted by: omega moon watch Mar 11, 2010 @ 20:26

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Leave a message

(Required)
(Required and not displayed)
(Optional)
obfuscated letters Enter the text shown in the image