Lan*_*rts 38 windows quotes sed batch-file echo
我有一个我正在创建的Windows批处理文件,但我必须ECHO一个大的复杂字符串,所以我必须在任何一端放双引号.问题是引号也正在被我正在写入的文件进行ECHO.你如何回收这样的字符串并删除引号?
更新:
我花了最近两天的时间来研究这个问题,最后能够将这些东西融合在一起.理查德的回答是剥离引号,但即使我把ECHO放在子程序中并直接输出字符串,Windows仍然挂在字符串中的字符上.我会接受理查德的回答,因为它回答了问题.
我最终使用了Greg的sed解决方案,但由于sed/windows错误/功能而不得不修改它(它没有帮助它没有文档).在Windows中使用sed有一些注意事项:你必须使用双引号而不是单引号,你不能直接转义字符串中的双引号,你必须结束字符串,使用^进行转义(所以^"然后,有人指出,如果你将输入管道输入到sed,那么管道中有一个管道存在错误(我没有得到验证,因为在我的最终解决方案中,我刚发现一种不在字符串中间包含所有引号的方法,只是删除了所有引号,我永远不能让endquote自行删除.)感谢所有的帮助.
Ric*_*d A 55
call命令内置了此功能.引用调用的帮助:
Substitution of batch parameters (%n) has been enhanced. You can
now use the following optional syntax:
%~1 - expands %1 removing any surrounding quotes (")
Run Code Online (Sandbox Code Playgroud)
这是一个原始的例子:
@echo off
setlocal
set mystring="this is some quoted text"
echo mystring=%mystring%
call :dequote %mystring%
echo ret=%ret%
endlocal
goto :eof
:dequote
setlocal
rem The tilde in the next line is the really important bit.
set thestring=%~1
endlocal&set ret=%thestring%
goto :eof
Run Code Online (Sandbox Code Playgroud)
输出:
C:\>dequote
mystring="this is some quoted text"
ret=this is some quoted text
Run Code Online (Sandbox Code Playgroud)
我应该将'环境变量隧道'技术(endlocal&set ret =%thestring%)归功于Tim Hill,'Windows NT Shell Scripting'.这是我发现的唯一能够解决任何深度批处理文件的书.
小智 10
您可以使用%var:x=y%替换所有施工x用y.
看看这个例子它能做什么:
set I="Text in quotes"
rem next line replaces " with blanks
set J=%I:"=%
echo original %I%
rem next line replaces the string 'in' with the string 'without'
echo stripped %J:in=without%
Run Code Online (Sandbox Code Playgroud)