0

I am a Linux guy and trying to learn batch scripting.

I have the following requirement to manipulate the string.

set string=1.23.10xxxx2

I wanted to remove the alpha characters from the above string.

i need the output as 1.23.102 or 1.23.10, both outputs are fine to me, could anyone please help me.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
user1642408
  • 11
  • 1
  • 6
  • batch has pretty limited/clumsy string-manipulation. Coming from linux, you will be disappointed. Can you use python or something instead? Or install [unixutils](http://sourceforge.net/projects/unxutils/) and use sed? – Blorgbeard Jul 08 '13 at 20:52
  • Look at `set /?` for the limited range of stuff you can do with batch though. – Blorgbeard Jul 08 '13 at 20:54

3 Answers3

3
@echo off
set remove=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

set string=1.23.10xxxx2
for /F "tokens=1,2 delims=%remove%" %%a in ("%string%") do (
   echo Part before removed chars: %%a
   echo Part after  removed chars: %%b
   echo Both parts: %%a%%b
)
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • @Endoro: Yes, or add the upper-case letters in the first for... I just fixed the solution this way! – Aacini Jul 09 '13 at 02:43
2

If the format has a consistent length, then you can simply use sub-string operations.

To get 1.23.10:

set "string=%string:~0,7%"

To get 1.23.102:

set "string=%string:~0,7%%string:~-1%"

If you are simply removing the character x, then use search and replace (always case insensitive):

set "string=%string:x=%"

All of the above are described in the help, accessed by help set or set /?.

But I suspect that none of the above will meet your needs. There is nothing built into batch to conveniently search and replace ranges of characters. You could use a FOR loop to iteratively search and replace each letter. This requires delayed expansion because normal expansion occurs at parse time, and the entire FOR construct is parsed in one pass.

setlocal enableDelayedExpansion
for %%C in (
  A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
) do set "string=!string:%%C=!"

The above works, but it is relatively inefficient.

There are any number of 3rd party tools that could efficiently solve the problem. But non-standard executables are forbidden in some environments. I've written a hybrid batch/JScript utility called REPL.BAT that works extremely well for this problem. It works on any modern Windows machine from XP onward. Click the link to get the script. Full documentation is built into the script.

Assuming REPL.BAT is either in your current directory, or better yet, somewhere in your PATH, then the following will work:

for /f "eol=a delims=" %%S in ('repl "[a-zA-Z]" "" s string') do set "string=%%S"
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
1

You can use GNUWin32 sed:

@ECHO OFF &SETLOCAL
set "string=1.23.10xxxx2"
FOR /f %%a IN ('echo %string% ^| sed "s/[a-zA-Z]\+//"') DO set "newstring=%%a"
ECHO %newstring%
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • well i need a windows batch script where i can't have sed to run. – user1642408 Jul 08 '13 at 21:17
  • i am getting the following error.'sed' is not recognized as an internal or external command, operable program or batch file. ECHO is off. – user1642408 Jul 08 '13 at 21:18
  • `sed` is not part of standard Windows installation. You can download it from the link in my answer. And you can use it with a batch script, please look at my answer with the script. – Endoro Jul 08 '13 at 21:21