简体   繁体   English

禁用sed中反斜杠的转义功能

[英]disable the escaping function of the backslash in sed

In GNU sed (and almost all other distributions I know of): 在GNU sed(以及我所知道的几乎所有其他发行版)中:

SET VAR=C:\nyc
sed "s/PATH/%VAR%/g" file.txt

causes any occurrence of PATH to be replaced with a new line followed by "yc". 导致任何出现的PATH被替换为新行后跟“yc”。 In my case VAR is always a user supplied literal (it's a Windows path). 在我的情况下,VAR始终是用户提供的文字(它是Windows路径)。 How can I disable the default interpreting of '\\'? 如何禁用'\\'的默认解释? (I know about hacks like saving VAR in a file and replacing all "\\" with "\\\\" in VAR and then passing it to sed) (我知道在文件中保存VAR并在VAR中将所有“\\”替换为“\\\\”然后将其传递给sed)

you can modify %var% in the Windows command shell: 您可以在Windows命令shell中修改%var%

>type file
PATH
PATH

>set var=C:\nyc

>sed "s/PATH/%VAR%/g" file
C:
yc
C:
yc

>set var=%var:\=\\%

>echo %var%
C:\\nyc

>sed "s/PATH/%VAR%/g" file
C:\nyc
C:\nyc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM