Page 1 of 1
Replace a substring using Set command
Posted: 26 Feb 2009 17:30
by suitto
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
Posted: 26 Feb 2009 21:58
by _m
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
Posted: 26 Feb 2009 22:47
by suitto
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.
Posted: 17 Mar 2009 13:02
by JustJoe
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
Posted: 19 Mar 2009 10:06
by avery_larry
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)
Posted: 19 Mar 2009 15:38
by *SCRIPTER*
----