中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-07 08:38
48,040 topics / 350,125 posts / today 0 new / 48,252 members
DOS批处理 & 脚本技术(批处理室) » How can sed use DOS environment variables?
Printable Version  1,209 / 9
Floor1 axolo Posted 2007-10-30 16:35
新手上路 Posts 7 Credits 18
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 ]
Floor2 lxmxn Posted 2007-10-31 04:29
版主 Posts 4,938 Credits 11,386
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:
Floor3 axolo Posted 2007-10-31 08:21
新手上路 Posts 7 Credits 18
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
Floor4 axolo Posted 2007-10-31 13:17
新手上路 Posts 7 Credits 18
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?
Floor5 abcd Posted 2007-10-31 13:20
银牌会员 Posts 739 Credits 1,436
Floor6 axolo Posted 2007-10-31 15:12
新手上路 Posts 7 Credits 18
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
Floor7 abcd Posted 2007-10-31 15:18
银牌会员 Posts 739 Credits 1,436
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
Floor8 axolo Posted 2007-10-31 16:32
新手上路 Posts 7 Credits 18
That is, I have a text file, its contents are as follows:


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



\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 ]
Floor9 abcd Posted 2007-10-31 16:45
银牌会员 Posts 739 Credits 1,436
Floor10 zhflwx Posted 2008-01-17 22:40
新手上路 Posts 5 Credits 11
I learned something again, hehe.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023