中国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-09-24 00:27
47,812 topics / 349,917 posts / today 0 new / 48,274 members
DOS批处理 & 脚本技术(批处理室) » How to express special characters in sed?
Printable Version  1,801 / 10
Floor1 jtyuer Posted 2006-08-15 14:30
初级用户 Posts 29 Credits 73
I want to use sed to modify a passage in the txtsetup.sif file, changing
SetupSourcePath = "\"
to
SetupSourcePath = "\WINXP\ACER\"
I know that \ is an escape character, but I have tried many methods and all prompt errors. The following are what I have tried:
sed -i "s/SetupSourcePath = "\"/SetupSourcePath = "\WINXP\ACER\"/g" txtsetup.sif
sed -i "s/SetupSourcePath = "\\"/SetupSourcePath = "\\WINXP\\ACER\\"/g" txtsetup.sif
sed -i "s/SetupSourcePath = "\\\"/SetupSourcePath = "\\\WINXP\\\ACER\\\"/g" txtsetup.sif
sed -i "s/SetupSourcePath = "\/"/SetupSourcePath = "\/WINXP\/ACER\/"/g" txtsetup.sif
There are others, but each time it is executed, an error is prompted, generally saying that the s parameter is incorrect. If changed to
sed -i "s/SetupSourcePath/SetupSourcePathWINXP/g" txtsetup.sif
In this way, there is no problem, so I am sure that I expressed the \ special character in sed incorrectly. I hope the bosses can take a look and help correct this. Thank you everyone!
Floor2 jtyuer Posted 2006-08-16 11:26
初级用户 Posts 29 Credits 73
Guys, help out, please. Let's see how to modify the above.
Floor3 jtyuer Posted 2006-08-16 20:44
初级用户 Posts 29 Credits 73
Regardless of right or wrong, everyone give an opinion, please!!!!!
Floor4 无奈何 Posted 2006-08-16 21:29
荣誉版主 Posts 356 Credits 1,338
The problem of double quotes in CMD is quite a headache. A simple way is to use the ASCII code value 22 to represent " , which is converted to the regular expression as \x22. Please refer to the following approach:
Floor5 jtyuer Posted 2006-08-16 22:01
初级用户 Posts 29 Credits 73
Originally posted by Helplessness at 2006-8-16 21:29:
The problem of double quotes in CMD is quite frustrating. A simple way is to use the ASCII code value 22 to represent ",which is converted to the regular expression as \x22. Please refer to the following approach:
Floor6 无奈何 Posted 2006-08-16 22:12
荣誉版主 Posts 356 Credits 1,338
It tests well on my side. Post the error message. What version of SED are you using? If you want good support for the -i parameter, it is recommended to use GNU sed 4.14.

C:\TEST>sed "s/SetupSourcePath = \x22\\/&WINXP\\ACER\\/" t.txt
SetupSourcePath = "\WINXP\ACER\"
C:\TEST>sed "s/SetupSourcePath = \x22\\\x22/SetupSourcePath = \x22\\WINXP\\ACER\
\\x22/" t.txt
SetupSourcePath = "\WINXP\ACER\"
C:\TEST>
Floor7 jtyuer Posted 2006-08-16 23:44
初级用户 Posts 29 Credits 73
The result using GNU Sed 4.1.4 recommended by GM:
Floor8 jtyuer Posted 2006-08-17 00:06
初级用户 Posts 29 Credits 73
Thanks to the great help of Wu Nai He, the problem has been solved. It turned out to be the problem of the sed version I was using, which was probably too old. Thanks again to the moderator!
Floor9 flyingphf Posted 2008-03-29 14:41
中级用户 Posts 142 Credits 392
I want to ask, if there is/ how to represent it
Floor10 ThinKing Posted 2008-03-29 14:49
中级用户 Posts 207 Credits 471
Originally posted by flyingphf at 2008-3-29 02:41 PM:
I want to ask, if there is /, how to represent it?

Change a delimiter.

eg : sed "s~http://~ftp://~g" ufile

[ Last edited by ThinKing on 2008-3-29 at 02:51 PM ]
Floor11 sylovanas Posted 2008-03-29 15:00
初级用户 Posts 42 Credits 107
/也可以 ascii /是2F
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023