Variable search and replace hell

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

Variable search and replace hell

Post by MigrationUser »

17 Feb 2007 13:03
bluesxman


Anyone got any bright ideas how one could overcome this little problem:

Code: Select all

@echo off

set x=asd*asd

echo:This works just fine: %x:asd*=ASD%
pause
echo:This sort of does, but might not look quite as intended: %x:*asd=ASD%
pause
echo:But what if I just want to replace the "*"?
pause
echo:Sadly, this doesn't work at all: %x:^*=ASD%
pause
echo:And this is just HORRIBLE:
echo:%x:*=ASD%
echo:You won't even get as far as this line!
Lovely, huh?

~bxm

cmd | *sh | ruby | chef

----------------------------

#2 17 Feb 2007 13:23
Simon Sheppard


for /f "tokens=1,2 delims=*" %%G in ("asd*asd") do echo %%G~%%H

----------------------------

#3 17 Feb 2007 15:23
bluesxman


Yeah I've worked around it with something like that before, but what if, say, I had variable numbers of consecutive "*", the number of which I wanted to preserve in the substitution? That there "for" command would reduce them all to a singular.

Or an unknown/very large number of "*"? OK, OK I'm pushing the bounds of likelihood now, but still...

cmd | *sh | ruby | chef

----------------------------

#4 17 Feb 2007 15:33
Simon Sheppard


For those cases I think FindStr /L is the tool to look at

If you dont want to write a text file, you can just pipe text into FindStr.

----------------------------

#5 19 Feb 2007 12:28
bluesxman


Hmmm ... I'm not sure how a literal string search is going to help out searching and replacing. Ugh, why can't Microsoft ship Windows with a half-arsed "sed" rip off? After all, they've managed to include a half-arsed "egrep" rip off...

cmd | *sh | ruby | chef

----------------------------

#6 02 Mar 2007 19:48
Dan9999


Try this. Problem is that if you have quotes in there it won't work. You have to remove them first.

If you want to remove quotes, I guess it's SET OrigString=%OrigString:"=%

Hope it works

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set OrigString=asd*asd*asd;\;
set ResultString=
set Searchchar=*
set ReplChar=+

SET /A counter=0
:tagSTART
set tmpChar=!OrigString:~%counter%,1!
if not defined tmpChar goto tagEND
if  "%tmpChar%" == "%Searchchar%" SET tmpChar=%ReplChar%
set ResultString=%ResultString%%tmpChar%
set /A counter=%counter% + 1
goto tagStart
:tagEND
echo %ResultString%
----------------------------

#7 02 Mar 2007 20:07
Simon Sheppard


Very nice

I still cant believe Microsoft dont have a utility for this, fingers crossed for the vista resource kit next month.

----------------------------

#8 09 Mar 2007 19:24
bluesxman


Sorry, I never said thanks -- thanks!

Not tried your code as yet, but I like what you've done there.

As for quotes, I'd tend to switch them out for a character that's unlikely to be in the string (or determine one with some jiggery pokery) and put them back in at the end.

Also, I'm not a fan of using "goto" in any other context than "goto :EOF". A hang over from the teachings of Mr Bird, my high school IT teacher, many years ago. I only use it when I have no other viable options, so I'd go something like this:

Code: Select all

if not defined tmpChar goto :EOF
if  "%tmpChar%" == "%Searchchar%" SET tmpChar=%ReplChar%
set ResultString=%ResultString%%tmpChar%
set /A counter=%counter% + 1
call :tagStart

echo %ResultString%
But that's just me.

Last edited by bluesxman (09 Mar 2007 19:29)
Post Reply