China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-12 03:09
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Discussion] Is there a convenient method for converting DOS and UNIX file formats? View 1,515 Replies 12
Original Poster Posted 2008-01-10 14:13 ·  中国 上海 东方有线
中级用户
★★
Credits 321
Posts 135
Joined 2007-02-04 04:48
19-year member
UID 78578
Gender Male
Status Offline
Actually, I only need the one from UNIX to DOS, that is, '\n' -> '\r\n'

With the help of sed, the following way can achieve the effect:

sed -e "s/$/\r/" myunix.txt > mydos.txt 




However, now I want to ask if there is a simpler method, preferably without using third-party tools.


I already know the principle. The following is excerpted from: http://hi.baidu.com/dbsuit/blog/item/511eb409f9abe7266b60fba8.html


Conversion between Windows/DOS and Unix file formats
2007-06-21 13:25

Windows/DOS and Unix file formats are different

First, clarify a few symbols
*******************
0A LF ^J Newline
0D CR ^M Carriage return
*******************

In DOS/Windows text files, CR (carriage return \r) and LF (newline \n) are used.
In the case of the end of a line in the file, it is '\r\n'

Unix text only uses the newline character, with a newline (\n) at the end of the line, that is, '\n'
So C programs edited under Windows and compiled under Unix will have a "No end of newline" Warning

Conversion between the two file formats
Unix -> Dos
'\n' -> '\r\n'

//////////////////////////////////////////

while ( (ch = fgetc(in)) != EOF )
{
if ( ch == '\n' )
putchar('\r');
putchar(ch);
}

//////////////////////////////////////////

Just add a '\r' character before the '\n' that appears in the Unix file

Unix <- DOS
'\n' <- '\r\n'

The situation from Dos to Unix is more complicated. You can't just remove the '\r' read from the file.
Because there may be an embedded carriage return symbol at the end of the text line in the Dos file, this situation occurs in impact printers.
So before conversion, you need to judge whether '\r' and '\n' appear simultaneously.
If they appear simultaneously, remove '\r'
If they do not appear simultaneously, keep '\n'

//////////////////////////////////////////

cr_flag = 0; /* No CR encountered yet */
while ( (ch = fgetc(in)) != EOF )
{
if ( cr_flag && ch != '\n' ) {
/* This CR did not preceed LF */
putchar('\r');
}
if ( !(cr_flag = (ch == '\r')) )
putchar(ch);
}

//////////////////////////////////////////



[ Last edited by honghunter on 2008-1-10 at 02:15 PM ]
Floor 2 Posted 2008-01-10 15:53 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 3 Posted 2008-01-10 16:12 ·  中国 上海 东方有线
中级用户
★★
Credits 321
Posts 135
Joined 2007-02-04 04:48
19-year member
UID 78578
Gender Male
Status Offline
Originally posted by Must Love at 2008-1-10 03:53 PM:
Unix -> Dos

type Unix.txt>dos.txt
That's it

I did this, but it's still in UNIX format,郁闷.
Floor 4 Posted 2008-01-10 16:40 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 5 Posted 2008-01-10 16:45 ·  中国 上海 东方有线
中级用户
★★
Credits 321
Posts 135
Joined 2007-02-04 04:48
19-year member
UID 78578
Gender Male
Status Offline
The source file only had line breaks, and the new file became "ECHO is on."
Floor 6 Posted 2008-01-10 16:55 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Floor 7 Posted 2008-01-10 17:00 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
将more myunix.txt>mydos.txt按照要求直接输出即可,因为这本身是英文相关操作命令,无需翻译,所以输出:more myunix.txt>mydos.txt
Floor 8 Posted 2008-01-10 20:54 ·  中国 上海 普陀区 电信
中级用户
★★
Credits 321
Posts 135
Joined 2007-02-04 04:48
19-year member
UID 78578
Gender Male
Status Offline
Thank you all. I'll try it again when I get to the company tomorrow.
Floor 9 Posted 2009-06-01 13:08 ·  中国 四川 成都 电信
初级用户
★★
Credits 118
Posts 66
Joined 2006-08-18 16:04
19-year member
UID 60797
Gender Male
Status Offline
Originally posted by lxmxn at 2008-1-10 17:00:
more myunix.txt>mydos.txt


Solved a problem that had troubled me for a long time. Thanks to the moderator.

It's amazing. I didn't know more had this function.

[ Last edited by weasel on 2009-6-2 at 06:47 ]
Floor 10 Posted 2010-07-19 20:56 ·  中国 辽宁 葫芦岛 联通
新手上路
Credits 8
Posts 8
Joined 2010-07-17 21:27
16-year member
UID 170660
Gender Male
Status Offline
Originally posted by weasel at 2009-6-1 13:08:

Solved a problem that had been bothering me for a long time. Thanks to the moderator.

It's amazing, I didn't know more had this function.

[ Last edited by weasel on 2009-6-2 at 06:47 ]



Similarly, if in a Unix system, using more dosfile>unixfile can also convert Windows format text to Unix format text.
Floor 11 Posted 2010-07-20 13:24 ·  新加坡
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Which version of Unix did you test in?
Floor 12 Posted 2010-07-20 16:05 ·  中国 辽宁 葫芦岛 联通
新手上路
Credits 8
Posts 8
Joined 2010-07-17 21:27
16-year member
UID 170660
Gender Male
Status Offline
Originally posted by HAT at 2010-7-20 13:24:
Which version of Unix did you test in?



It's not testing but using. It's the SCO Unix 5.0.5 version
Floor 13 Posted 2010-07-21 10:39 ·  新加坡
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
You're really lucky, all my tests in the following systems failed:
Ubuntu, Solaris, AIX, FreeBSD
Forum Jump: