syntax to replace a search string containing =

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

syntax to replace a search string containing =

#1 Post by sambasiva » 02 Jun 2013 10:43

Hi Batch Gurus,


String="Hi how is the day=100 for you"

My requirement is to replace day=100 with day=222 in the above string.


I am not able to figure out the syntax of the same.

Could someone please help me with this...

Thanks in Advance,
Sambasiva

Squashman
Expert
Posts: 4222
Joined: 23 Dec 2011 13:59

Re: syntax to replace a search string containing =

#2 Post by Squashman » 02 Jun 2013 14:07

Why are you trying to replace the word day and the equals sign with the same thing? Just replace the 100 with 222.

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: syntax to replace a search string containing =

#3 Post by foxidrive » 02 Jun 2013 20:19

Here you go:

Code: Select all

@echo off
set "String=Hi how is the day=100 for you"

call :Search_and_replace "%string%" "(.*day=)[0-9]*(.*)" "$1222$2"

for /f "delims=" %%c in ('cscript /nologo _.vbs') do set "output=%%c"
)

echo "%output%"

del _.vbs
goto :EOF

:Search_and_replace
set s=regex.replace("%~1","%~3")
 >_.vbs echo set regex=new regexp
>>_.vbs echo regex.global=True
>>_.vbs echo regEx.IgnoreCase=True           
>>_.vbs echo regex.pattern="%~2"
>>_.vbs echo wscript.stdOut.write %s%

Aacini
Expert
Posts: 1680
Joined: 06 Dec 2011 22:15
Location: México City, México

Re: syntax to replace a search string containing =

#4 Post by Aacini » 02 Jun 2013 21:27

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "String=Hi how is the day=100 for you"

REM My requirement is to replace day=100 with day=222 in the above string.
set old=day=100
set new=day=222
for /F "tokens=1,2 delims==" %%a in ("%String%") do (
   rem Char next to %%a in line below is Ascii-255, not space
   set "left=%%a "
   set "right=%%b"
)
for /F "tokens=1,2 delims==" %%a in ("%old%") do (
   for /F "tokens=1,2 delims==" %%x in ("%new%") do (
      rem Char next to %%a in line below is Ascii-255, not space
      set "String=!left:%%a =%%x=!!right:*%%b=%%y!"
   )
)
echo %String%


Antonio

sambasiva
Posts: 43
Joined: 02 Jun 2013 10:25

Re: syntax to replace a search string containing =

#5 Post by sambasiva » 02 Jun 2013 22:53

Both the solutions worked...
Thanks alot for the quick help...

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: syntax to replace a search string containing =

#6 Post by Endoro » 03 Jun 2013 02:24

Aacini wrote:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "String=Hi how is the day=100 for you"

REM My requirement is to replace day=100 with day=222 in the above string.
set old=day=100
set new=day=222
for /F "tokens=1,2 delims==" %%a in ("%String%") do (
   rem Char next to %%a in line below is Ascii-255, not space
   set "left=%%a "
   set "right=%%b"
)
for /F "tokens=1,2 delims==" %%a in ("%old%") do (
   for /F "tokens=1,2 delims==" %%x in ("%new%") do (
      rem Char next to %%a in line below is Ascii-255, not space
      set "String=!left:%%a =%%x=!!right:*%%b=%%y!"
   )
)
echo %String%
Very nice :) !

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: syntax to replace a search string containing =

#7 Post by Ocalabob » 03 Jun 2013 07:30

String="Hi how is the day=100 for you"
My requirement is to replace day=100 with day=222 in the above string.

Code: Select all

@echo off
Echo "Hi how is the day=100 for you" |SED "s/day=100/day=222/"
Pause>nul

Find and Replace is a 'major food group' for SED. :)

Post Reply