Converting DOS/UNIX and vice-versa

Ask a question
There are several utility software to convert text files from UNIX or Linux to DOS operating systems and vice-versa; however, it always helps to know the manual conversion. 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.

Intro


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


Simply 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
Jean-François Pillou

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

Learn more about the CCM team

Related