1
Thanks

A few words of thanks would be greatly appreciated.

Converting DOS/UNIX and vice-versa

There are several softwares to convert text files from UNIX or Linux to DOS operating systems and vice-versa. However, it always helps to know the manual conversion.




Intro

In shell programming languages like UNIX or Linux the text files conclude with a new line operator "\n" also known as the line feed and its ASCII code is 0A.

A DOS Text file concludes a line by the carriage return or the entry key "\r": its ASCII code is 0D.

The lines in the DOS end with CRLF or with "\r\n". To convert this DOS text into UNIX or Linux , erase the "\r": you can also use ASCII codes if you are using GNU-sed version.

From UNIX to DOS you may add the "\r" or else use GNU-sed notations symbolically, using the ASCII codes.

Though there are (dos2unix and unix2dos) utility programs capable of performing this task, here is a simple tip on how to manually make these features.

Conversion DOS to UNIX / UNIX to DOS

The text files under UNIX end their line with the symbol "\n" (called Line Feed and noted LF, ASCII code = 0A).

Text files under DOS by a "line", end their line with the symbol "\r"
(called Carriage Return and noted CR, ASCII 0D). Thus, every line in a DOS file ends with a CRLF sequence, or \r\n.

Conversion from DOS to UNIX

Delete the "\r" (carriage return) at the end of the line.The "\r" is symbolically represented by "^M", which is obtained by the following sequence of keys "CTRL-V" + "CTRL-M":

sed 's/^M$//' file


Note: with the GNU-sed(gsed 3.02.80) version, we can use the ASCII notation:

sed 's/\x0D$//' file

Conversion from UNIX to DOS

Just do the opposite of the previous command, namely (the "^M" being entered in the same way (CTRL-V + CTRL-M)):

sed 's/$/^M/' file


Note: with the GNU-sed(gsed 3.02.80) version, we can use the symbolic notation "\r":
sed 's/$/\r/' file


Image: © Unsplash
1
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Related

This document, titled « Converting DOS/UNIX and vice-versa », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).