We have moved to www.dataGenX.net, Keep Learning with us.

Friday, December 30, 2011

dos2unix AND unix2dos : Converting between DOS and UNIX

Here r the 2 lines from a DOS file, atul, viewed on a unix system with the vi editor :

Line 1^M
Line 2^M


$ od -bc atul  
{ this command is used to check the CR-LF in file }

There is a ^M ( ctrl - m ) representing the CR sequence at the end of each line. An octal dump confirm this :
The CR-LF combination is represention by the octal value 015 -012 and the escape sequesnce \r\n. Conversion of this file to UNIX is just a simple matter of removing the \r. This is often done automatically when downloading a UNIX file from windows machine using ftp, but sometimes u have to do it yourself.

$ dos2unix atul.dos atul 
$ unix2dos atul.dos atul


* unix2dos inserts CR before every LF, and thus increase the file size by the no of line in the file.
* You can use dos2unix to delete the ^M char that occurs at the end of every line in the file typescript.
* If u view the unix file atul on windows using notepad but without performing the conversion then you'll see a single line : Line 1Line 2. In fact, whenever you see a single line on a windows machine that should have been multiple line.
* Never perform this conversion on a binary file.




No comments :

Post a Comment