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-06-27 12:29
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to express special characters in sed? View 1,661 Replies 10
Original Poster Posted 2006-08-15 14:30 ·  中国 江苏 常州 金坛区 电信
初级用户
Credits 73
Posts 29
Joined 2006-08-13 21:22
19-year member
UID 60447
Status Offline
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!
Floor 2 Posted 2006-08-16 11:26 ·  中国 江苏 常州 金坛区 电信
初级用户
Credits 73
Posts 29
Joined 2006-08-13 21:22
19-year member
UID 60447
Status Offline
Guys, help out, please. Let's see how to modify the above.
Floor 3 Posted 2006-08-16 20:44 ·  中国 江苏 常州 金坛区 电信
初级用户
Credits 73
Posts 29
Joined 2006-08-13 21:22
19-year member
UID 60447
Status Offline
Regardless of right or wrong, everyone give an opinion, please!!!!!
Floor 4 Posted 2006-08-16 21:29 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
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:

1、sed "s/SetupSourcePath = \x22\\\x22/SetupSourcePath = \x22\\WINXP\\ACER\\\x22/" txtsetup.sif
2、sed "s/SetupSourcePath = \x22\\/&WINXP\\ACER\\/" txtsetup.sif
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 5 Posted 2006-08-16 22:01 ·  中国 江苏 常州 金坛区 电信
初级用户
Credits 73
Posts 29
Joined 2006-08-13 21:22
19-year member
UID 60447
Status Offline
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:

1、sed "s/SetupSourcePath = ...

Thank you for the moderator's reply! But I tried it, and neither of the two commands worked. Could it be a problem with my version of sed?
Floor 6 Posted 2006-08-16 22:12 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
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>
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 7 Posted 2006-08-16 23:44 ·  中国 江苏 常州 金坛区 电信
初级用户
Credits 73
Posts 29
Joined 2006-08-13 21:22
19-year member
UID 60447
Status Offline
The result using GNU Sed 4.1.4 recommended by GM:
Floor 8 Posted 2006-08-17 00:06 ·  中国 江苏 常州 金坛区 电信
初级用户
Credits 73
Posts 29
Joined 2006-08-13 21:22
19-year member
UID 60447
Status Offline
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!
Floor 9 Posted 2008-03-29 14:41 ·  中国 上海 奉贤区 电信
中级用户
★★
Credits 392
Posts 142
Joined 2007-06-10 09:48
19-year member
UID 90856
Gender Male
Status Offline
I want to ask, if there is/ how to represent it
Floor 10 Posted 2008-03-29 14:49 ·  中国 湖北 武汉 电信
中级用户
★★
Credits 471
Posts 207
Joined 2007-05-03 14:53
19-year member
UID 87369
Gender Male
Status Offline
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 ]
Floor 11 Posted 2008-03-29 15:00 ·  中国 台湾 宜兰县 中华电信
初级用户
Credits 107
Posts 42
Joined 2007-09-03 16:43
18-year member
UID 96475
Gender Male
Status Offline
/也可以 ascii /是2F
Forum Jump: