除了SED,如何更换等号? 如何在字符串替换中使用字符串变量?
考虑这个例子:
For /F "tokens=*" %%B IN (test.txt) DO (
SETLOCAL ENABLEDELAYEDEXPANSION
SET t=is
SET old=%%B
SET new=!old:t=!
ECHO !new!
ENDLOCAL
)
:: SET new=!old:==!
两个问题:
首先,我不能在!:=!。
中使用变量%t% SET t=is
SET old=%%B
SET new=!old:t=!
其次,我无法替换命令行中的等号
SET new=!old:==!
答案 0 :(得分:3)
我自己为此创建了一个简单的解决方案,也许可以帮助某人。
缺点(或优点,取决于您要执行的操作)是一个接一个地处理多个等号,就像一个等号一样。 (例如:“ str == ing”给出的输出与“ str = ing”相同)
@echo off
set "x=this is=an test="
echo x=%x%
call :replaceEqualSign in x with _
echo x=%x%
pause&exit
:replaceEqualSign in <variable> with <newString>
setlocal enableDelayedExpansion
set "_s=!%~2!#"
set "_r="
:_replaceEqualSign
for /F "tokens=1* delims==" %%A in ("%_s%") do (
if not defined _r ( set "_r=%%A" ) else ( set "_r=%_r%%~4%%A" )
set "_s=%%B"
)
if defined _s goto _replaceEqualSign
endlocal&set "%~2=%_r:~0,-1%"
exit /B
如您所见,您可以使用以下函数:
call :replaceEqualSign in variableName with newString
答案 1 :(得分:2)
如果%% B包含setlocal enableDelayedExpansion
,则应在旧的= %% B分配后移动!
。
通过使用另一个FOR变量
,循环内的“t”问题很容易解决For /F "tokens=*" %%B IN (test.txt) DO (
SET t=is
SET old=%%B
SETLOCAL ENABLEDELAYEDEXPANSION
for /f %%T in ("!t!") do SET new=!old:%%T=!
ECHO !new!
ENDLOCAL
)
没有用于替换=
的简单本机批处理解决方案。你可以逐个字符地遍历字符串,但这很慢。您最好的选择可能是切换到VBScript或JScript,或使用非本机实用程序。
如果您真的想使用纯Windows批处理命令执行此操作,http://www.dostips.com/forum/viewtopic.php?t=1485
有几个有趣的想法答案 2 :(得分:1)
为什么不使用Edlin?我找不到用一个初始文件做这个的方法而且Edlin没有错误,但是用NUL忽略它们: 扼杀,TYPE%0包括整个文件,即使在=和!之间存在文件结尾符号,在运行后使用批处理文件上的TYPE将不会以相同的方式工作。
@ECHO OFF
GOTO skip
1,1r=!
e
:skip
SET "new==old============="
ECHO %new% > %TEMP%\var.tmp
TYPE %0 > %TEMP%\edlin.tmp
EDLIN %TEMP%\var.tmp < %TEMP%\edlin.tmp > NUL:
SET /P newnew=<%TEMP%\VAR.TMP
ECHO %newnew%
ERASE %TEMP%\VAR.TMP
ERASE %TEMP%\VAR.BAK
ERASE %TEMP%\edlin.tmp
答案 3 :(得分:1)
更新:最新版本的脚本位于:https://sf.net/p/contools(脚本:https://sf.net/p/contools/contools/HEAD/tree/trunk/Scripts/Tools/std/)
您可以使用某些序列来临时替换占位符,例如?00,?01,?02和?03。我基本上使用这些脚本集:
<强> replace_sys_chars.bat:强>
@echo off
rem Description:
rem Script to replace ?, !, %, and = characters in variables by respective
rem ?00, ?01, ?02 and ?03 placeholders.
setlocal DISABLEDELAYEDEXPANSION
set "__VAR__=%~1"
if "%__VAR__%" == "" exit /b 1
rem ignore empty variables
call set "STR=%%%__VAR__%%%"
if "%STR%" == "" exit /b 0
set ?01=!
call set "STR=%%%__VAR__%:?=?00%%"
set "STR=%STR:!=?01%"
setlocal ENABLEDELAYEDEXPANSION
set STR=!STR:%%=?02!
set "STR_TMP="
set INDEX=1
:EQUAL_CHAR_REPLACE_LOOP
set "STR_TMP2="
for /F "tokens=%INDEX% delims== eol=" %%i in ("/!STR!/") do set STR_TMP2=%%i
if "!STR_TMP2!" == "" goto EQUAL_CHAR_REPLACE_LOOP_END
set "STR_TMP=!STR_TMP!!STR_TMP2!?03"
set /A INDEX+=1
goto EQUAL_CHAR_REPLACE_LOOP
:EQUAL_CHAR_REPLACE_LOOP_END
if not "!STR_TMP!" == "" set STR=!STR_TMP:~1,-4!
(
endlocal
endlocal
set "%__VAR__%=%STR%"
)
exit /b 0
<强> restore_sys_chars.bat:强>
@echo off
rem Description:
rem Script to restore ?, !, %, and = characters in variables from respective
rem ?00, ?01, ?02 and ?03 placeholders.
setlocal DISABLEDELAYEDEXPANSION
set "__VAR__=%~1"
if "%__VAR__%" == "" exit /b 1
rem ignore empty variables
call set "STR=%%%__VAR__%%%"
if "%STR%" == "" exit /b 0
setlocal ENABLEDELAYEDEXPANSION
set STR=!STR:?02=%%!
set STR=!STR:?03==!
(
endlocal
set "STR=%STR%"
)
set "STR=%STR:?01=!%"
set "STR=%STR:?00=?%"
(
endlocal
set "%__VAR__%=%STR%"
)
exit /b 0
示例:强>
@echo off
setlocal DISABLEDELAYEDEXPANSION
for /F "usebackq tokens=* delims=" %%i in ("test.txt") do (
set VALUE=%%i
call :PROCESS
)
exit /b 0
:PROCESS
if "%VALUE%" == "" exit /b 0
set "VALUE_=%VALUE%"
call replace_sys_chars.bat VALUE_
rem do variable arithmetic here as usual
if not "%VALUE_:?00=%" == "%VALUE_%" echo."%VALUE%" having ?
if not "%VALUE_:?01=%" == "%VALUE_%" echo."%VALUE%" having !
if not "%VALUE_:?02=%" == "%VALUE_%" echo."%VALUE%" having %%
if not "%VALUE_:?03=%" == "%VALUE_%" echo."%VALUE%" having =
rem restore it
call restore_sys_chars.bat VALUE_
echo "VALUE=%VALUE_%"
echo.---
<强>的test.txt:强>
111/222
AAA=BBB
CCC=%DDD%
EEE=!FFF!
FFF=?00?01?02?03
<强>结果:强>
"VALUE=111/222"
---
"AAA=BBB" having =
"VALUE=AAA=BBB"
---
"CCC=%DDD%" having %
"CCC=%DDD%" having =
"VALUE=CCC=%DDD%"
---
"EEE=!FFF!" having !
"EEE=!FFF!" having =
"VALUE=EEE=!FFF!"
---
"FFF=?00?01?02?03" having ?
"FFF=?00?01?02?03" having =
"VALUE=FFF=?00?01?02?03"
---
功能:
答案 4 :(得分:0)
我在另一篇文章中的回答,但它也适用于此:
有一种替代方案更容易。而不是传入包含等号的值,而是尝试使用冒号。然后,通过修改该值(冒号)的能力,您可以将其转换回等于。这是一个例子:
@echo off
set VALUE1=%1
set VALUE2=%VALUE1::==%
echo value1 = %VALUE1%
echo value2 = %VALUE2%
运行批处理文件时,请按以下方式调用:
C:\>myBatch name:someValue
输出结果为:
value1 = name:someValue
value2 = name=someValue
如果名称或值包含空格,则还有其他问题需要解决。您需要将整个字符串用双引号括起来。但是,那么你有需要摆脱它们的问题。这也可以这样处理:
@echo off
cls
set PARAM=%1
set BASE=%PARAM:"=%
set PAIR=%BASE::==%
rem Either of these two lines will do the same thing - just notice the 'delims'
rem for /f "tokens=1,2 delims=:" %%a in ("%BASE%") do set NAME=%%a & set VALUE=%%b
rem for /f "tokens=1,2 delims==" %%a in ("%PAIR%") do set NAME=%%a & set VALUE=%%b
for /f "tokens=1,2 delims=:" %%a in ("%BASE%") do set NAME=%%a & set VALUE=%%b
echo param = %PARAM%
echo base = %BASE%
echo pair = %PAIR%
echo name = %NAME%
echo value = %VALUE%
运行此批处理文件时:
C:\>myBatch "some name:another value"
输出将是:
param = "some name:another value"
base = some name:another value
pair = some name=another value
name = some name
value = another value
希望通过批处理文件帮助其他人争取胜利。
Mike V。
答案 5 :(得分:0)
我一直在研究这个问题,因为我需要在“ test = goingon”这样的字符串中去除= 我发现调用带有test = goingon作为参数的下一个批处理文件,在该批处理文件中有参数1,“ test”和2,“ goingon”。
所以: 批处理文件1:
@echo off
call test2.bat test=goingon
batchfile2:
echo arg1: %1
echo arg2: %2
结果:
arg1: test
arg2: goingon