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-07 07:52
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How can sed use DOS environment variables? View 1,207 Replies 9
Original Poster Posted 2007-10-30 16:35 ·  中国 浙江 杭州 电信
新手上路
Credits 18
Posts 7
Joined 2007-10-30 16:03
18-year member
UID 101194
Gender Male
Status Offline
1. Program: sed for dos
2. Files that need replacement: tool.ini,template.ini
3. Replace the string _SETPATH with the current path %CD%
4. Wrong command line: sed -i 's/_SETPATH/%CD%/g' tool.ini
5. Reason: it contains the % sign
6. It should probably work with: sed -i 's/_SETPATH/$CD/g' tool.ini

How can I make $CD equal to the current path specified by %CD% under DOS?
Can it replace multiple files in different paths at the same time?

Please advise, thanks.


sed -i "s/_SETPATH/%CD%/g" tool.ini

The result can be replaced, but the \ in the DOS path gets deleted
The result I get is c:windowssystem32
not the c:\windows\system32 that I want

How can this be solved》???

[ Last edited by axolo on 2007-10-30 at 08:36 PM ]
Floor 2 Posted 2007-10-31 04:29 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
RE axolo:

I guess your environment is a DOS environment or the command prompt under an NT-series system, so the line “sed -i 's/_SETPATH/%CD%/g' tool.ini” cannot be executed, and it will prompt “Unknown command: `''”, meaning the single quotes (') are causing the error. That's because in DOS or the command prompt environment, the system's default quote pair is double quotes (""), while single quotes are only recognized as ordinary characters. This is different from the shell under Linux.

So you can only use double quotes around the sed command. But the DOS path separator is the backslash (\), and backslash is sed's escape character. If you want to preserve this escape character, then you can only convert the \ in the original replacement string into \\, so one gets escaped and one remains.

Also, sed should be able to use wildcards. *.ini can represent all ini files in the current directory, so in the end it can be written like this:

@echo off
rem Convert \ in cd into \\:
set cd=%cd:\=\\%
sed -i "s/_SETPATH/%cd%/g" *.ini
Floor 3 Posted 2007-10-31 08:21 ·  中国 浙江 杭州 电信
新手上路
Credits 18
Posts 7
Joined 2007-10-30 16:03
18-year member
UID 101194
Gender Male
Status Offline
Many thanks
Thank you

I'm an old antique who's been around since DOS 3.1
Now I'm almost forgetting DOS and C

I didn't expect there would still be a place here with so many DOS experts gathered
Study hard
Thanks
Floor 4 Posted 2007-10-31 13:17 ·  中国 浙江 杭州 电信
新手上路
Credits 18
Posts 7
Joined 2007-10-30 16:03
18-year member
UID 101194
Gender Male
Status Offline
Another problem has come up: after sed does the replacement,
it actually turns DOS-format text into UNIX format
all the carriage return + newline \n get interpreted as carriage return \r

How can I keep the carriage return \n as-is and not have it changed to \r??
What should I do?
Floor 5 Posted 2007-10-31 13:20 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
Floor 6 Posted 2007-10-31 15:12 ·  中国 浙江 杭州 电信
新手上路
Credits 18
Posts 7
Joined 2007-10-30 16:03
18-year member
UID 101194
Gender Male
Status Offline
Originally posted by abcd at 2007-10-31 01:20 PM:
sed "s/$//"


I'm new here, so I don't quite understand.
Could you give the complete syntax???

Thanks
Floor 7 Posted 2007-10-31 15:18 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
Originally posted by axolo at 2007-10-31 03:12 PM:


I'm new here, so I don't quite understand.
Could you give the complete syntax???

Thanks

What I mean is replacing the unix newline character with DOS format
Floor 8 Posted 2007-10-31 16:32 ·  中国 浙江 杭州 电信
新手上路
Credits 18
Posts 7
Joined 2007-10-30 16:03
18-year member
UID 101194
Gender Male
Status Offline
That is, I have a text file, its contents are as follows:

\0x0D\0x0A
\0x0D\0x0A
\0x0D\0x0ACount=1
\0x0D\0x0A
\0x0D\0x0ADescription=New PHP script
\0x0D\0x0AFile=D:\app\EditPlus\php\template.php


set cd=%cd:\=\\%
sed "s/D:\\app\\EditPlus/%CD%/g" *.ini


\0x0A
\0x0A
\0x0ACount=1
\0x0A
\0x0ADescription=New PHP script
\0x0AFileE:\EditPlus\php\template.php


\0x0D\0x0A is DOS carriage return + newline
\0x0A is UNIX newline

D:\app\EditPlus was correctly replaced with E:\EditPlus, but the DOS carriage return + newline was also changed into UNIX newline.
I need to preserve \0x0D\0x0A

[ Last edited by axolo on 2007-10-31 at 04:35 PM ]
Floor 9 Posted 2007-10-31 16:45 ·  中国 浙江 衢州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
Floor 10 Posted 2008-01-17 22:40 ·  中国 江苏 苏州 电信
新手上路
Credits 11
Posts 5
Joined 2008-01-16 00:07
18-year member
UID 108724
Gender Female
Status Offline
I learned something again, hehe.
Forum Jump: