Under the section Replace a substring using string substitution,
an example is given as
set str=teh cat in teh hat
echo.%str%
set str=%str:teh=the%
echo.%str%
what if the substring to be replaced contains an equal sign? For instance.
try replace x=2 by y=3
set str=simple equation x=2
echo.%str%
set str=%str:x=2=y=3%
echo.%str%
gives incorrect output.
Any advise please
Replace a substring using Set command
Moderator: DosItHelp
Code: Select all
set str=simple equation x=2
echo.%str%
set str=%str:x=y%
set str=%str:2=3%
echo.%str%
Replace equal sign using string substitution
Hi _m,
Thanks for your reply.
Your slution works if both x and 2 are unique.
I should be more specific in posing the question
What I am trying to do is actually to search through a large text file,
to find a particular substring say nrel=0,
and change it to nrel=1.
In this case, 0 is not unique, but nrel=0 is unqiue.
Thanks again.
Thanks for your reply.
Your slution works if both x and 2 are unique.
I should be more specific in posing the question
What I am trying to do is actually to search through a large text file,
to find a particular substring say nrel=0,
and change it to nrel=1.
In this case, 0 is not unique, but nrel=0 is unqiue.
Thanks again.
Greetings:
Give this a try
Tested with WinXP Pro
U:\Batch-Variables>modstring "nrel=0" 0 1
Modified: "nrel=0" to: "nrel=1"
==============================================
@echo off
:: modstring.bat
::
:: arg %1 = String to modify
:: arg %2 = String to find
:: arg %3 = Replacement String
::
:: Initialize the local working environment
::
setlocal enableextensions enabledelayedexpansion
set Original=%~1
set Search=%~2
set Replace=%~3
:: The %string:STR1=STR2% function requires a little bit of funky code
:: to make it work when STR1 and STR2 are variables instead of constants.
:: The transition through the call statement is what actually makes it work
:: by expanding the variables properly.
::
:: The calling args are as follows:
::
:: arg1 = Command to execute that modifies the string
:: arg2 = Variable name that will contain the modified string
::
:: arg1 MUST be enclosed in quotes so it is passed as intended
:: arg2 enclosed in quotes for good measure
::
set Magic="set Modified=%%Original:%Search%=%Replace%%%"
call :ModifyString !Magic! "Modified"
echo.
echo Modified: "!Original!" to: !Modified!
echo.
endlocal
goto :End
:: ModifyString
::
:: arg %1 = Magically transformed Command to set the New string in arg %2
:: arg %2 = Target variable of command result
::
:ModifyString
::
:: Initialize the local working environment
setlocal enableextensions enabledelayedexpansion
::
:: Execute the Magically transformed command passed in arg %1 to modify the variable in arg %2
::
%~1
endlocal & set "%~2=%Modified%"
goto :End
:End
==============================================
This method doesn't work in all cases but will be able to do quite a few
cheers
Give this a try
Tested with WinXP Pro
U:\Batch-Variables>modstring "nrel=0" 0 1
Modified: "nrel=0" to: "nrel=1"
==============================================
@echo off
:: modstring.bat
::
:: arg %1 = String to modify
:: arg %2 = String to find
:: arg %3 = Replacement String
::
:: Initialize the local working environment
::
setlocal enableextensions enabledelayedexpansion
set Original=%~1
set Search=%~2
set Replace=%~3
:: The %string:STR1=STR2% function requires a little bit of funky code
:: to make it work when STR1 and STR2 are variables instead of constants.
:: The transition through the call statement is what actually makes it work
:: by expanding the variables properly.
::
:: The calling args are as follows:
::
:: arg1 = Command to execute that modifies the string
:: arg2 = Variable name that will contain the modified string
::
:: arg1 MUST be enclosed in quotes so it is passed as intended
:: arg2 enclosed in quotes for good measure
::
set Magic="set Modified=%%Original:%Search%=%Replace%%%"
call :ModifyString !Magic! "Modified"
echo.
echo Modified: "!Original!" to: !Modified!
echo.
endlocal
goto :End
:: ModifyString
::
:: arg %1 = Magically transformed Command to set the New string in arg %2
:: arg %2 = Target variable of command result
::
:ModifyString
::
:: Initialize the local working environment
setlocal enableextensions enabledelayedexpansion
::
:: Execute the Magically transformed command passed in arg %1 to modify the variable in arg %2
::
%~1
endlocal & set "%~2=%Modified%"
goto :End
:End
==============================================
This method doesn't work in all cases but will be able to do quite a few
cheers
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
You could search specifically for nrel=0 and THEN replace 0 with 1 -- this would assume there are no other 0's on the same line:
set str=simple equation x=0
echo.%str%|find /i "nrel=0"
if not errorlevel 1 set str=%str:0=1%
echo.%str%
Results in simple equation x=0 (No change)
set str=simple equation nrel=0
echo.%str%|find /i "nrel=0"
if not errorlevel 1 set str=%str:0=1%
echo.%str%
Results in simple equation nrel=1 (Modified)
set str=simple equation nrel=3
echo.%str%|find /i "nrel=0"
if not errorlevel 1 set str=%str:0=1%
echo.%str%
Results in simple equation nrel=3 (No change)
set str=simple equation x=0
echo.%str%|find /i "nrel=0"
if not errorlevel 1 set str=%str:0=1%
echo.%str%
Results in simple equation x=0 (No change)
set str=simple equation nrel=0
echo.%str%|find /i "nrel=0"
if not errorlevel 1 set str=%str:0=1%
echo.%str%
Results in simple equation nrel=1 (Modified)
set str=simple equation nrel=3
echo.%str%|find /i "nrel=0"
if not errorlevel 1 set str=%str:0=1%
echo.%str%
Results in simple equation nrel=3 (No change)