I need to split a string something like this; and try to get number
"Record Count/CC_NUMBER = 123".
I tried to many ways but crashed different things.
The number can be bigger ( 4 digit or more ) or smaller
Thanks in already for your help.
I need to split a string something like this; and try to get number
"Record Count/CC_NUMBER = 123".
I tried to many ways but crashed different things.
The number can be bigger ( 4 digit or more ) or smaller
Thanks in already for your help.
If the line length can contain any number of words, then split by =
and strip the whitespace, it will also not care if the =
has spaces or not before and after:
@echo off
set "line=this can be any length Record Count/CC_NUMBER = 123"
for /f "tokens=2 delims==" %%i in ("%line%") do set "var=%%i"
echo %var: =%
Additionally as suggested by Stephan a similar but shorter approach to the above:
@echo off
for /f "tokens=2 delims==" %%i in ("%line: =%") do echo %%i
If the line is with spaces and there the number is the 3rd token, as in your example:
@echo off
set "line=Record Count/CC_NUMBER = 123"
for /f "tokens=3" %%i in ("%line%") do echo %%i
If the line does not contain spaces we split by the =
instead:
@echo off
set "line=Record Count/CC_NUMBER=123"
for /f "tokens=2 delims==" %%i in ("%line%") do echo %%i
A possible way is to use a standard for
loop:
set "STR=Record Count/CC_NUMBER = 123"
for %%I in (%STR%) do set "VAL=%%I"
echo/%VAL%
Every SPACE as well as the =
-sign are treated as token separators, and every token is handled by the for
loop, and the last iteration assigns the numeric value to the variable.
This may fail when the string contains any of these characters: *
, ?
, <
, >
, "
, ^
, |
, &
, (
, )
.
One option would be to split the input by space, and then print/echo the third token:
for /f "tokens=3" %%G IN ("Record Count/CC_NUMBER = 123") DO echo %%G
See How to split a string by spaces in a Windows batch file? for more information.
Here is another solution using regex in powershell to extract a digit number from a string content from a TestFile.txt
@echo off & color 0A
Title Extract a Digit Number from String using Regex in Powershell
Set "InputFile=TestFile.txt"
Set psCmd="&{$content=Get-Content -Path '%InputFile%';[regex]$regex='\d+';$regex.Matches($content).value}"
Call :RunPS %psCmd% Number
Echo The Number extracted is N=%Number%
pause & Exit
::----------------------------------------------------------------------
:RunPS <PassPSCMD> <RetValue>
for /F "usebackq tokens=*" %%i in (`Powershell %1`) do set "%2=%%i"
Goto:eof
:: End of :RunPS function
::----------------------------------------------------------------------
This can be solved by using the echo
command, combine with cmd /u
removing non numbers in string by using Find + FindSTR +regex
in a loop For
.
cmd /u echo your string
.FindSTR
and RegEx
to get just numbers in this output process...
Use a For
to loop one by one character for filter your string in U n i c o d e
echo
output and remove the spaces by Find / v " "
, then apply another filter with FindSTR
to get only numbers by adding regex in FindSTR
and save the incremented loop output to an variable.
Loop output variable !_str! in unicode filter space and get numbers:
@echo off && setlocal EnableDelayedExpansion
set "_str= Record Count/CC_NUMBER = 01234 "
for /f %%n in ('cmd /u /c echo="!_str!"^|find /v " "^|findstr [0-9]
')do echo=%%n
:: echo=%%n == one by one nunber character in loop output:
:: -----------------------------------------------------------------
:: echo=%%n == output !_str! reults == 0
:: echo=%%n == output !_str! reults == 1
:: echo=%%n == output !_str! reults == 2
:: echo=%%n == output !_str! reults == 3
:: echo=%%n == output !_str! reults == 4
:: -----------------------------------------------------------------
Obs.: 1)
- Therefore, manipulating the output one by one, you can check whether the output character is a number and not, otherwise the present character in processing, nor will it be displayed, where is this capable possible to check/take any numbers occurs at any position in the variable/string, loop, so, there no need worry/work with delimiters.
Obs.: 2)
- This mechanical work's by using of the
EnableDelayedExpansion
in this cmd/bat process.
Loop output variable !_str! in Unicode incremented variable with numbers %%n:
@echo off && setlocal EnableDelayedExpansion
(set "_str=0R/ecord\-# ',? 1>|<2Count>/3$4]C{^5_6&7}NUMBER = *|8!9"
for /f %%n in ('cmd /u /c echo="!_str!"^|find /v " "^|findstr [0-9]
')do set "_l=!_l!%%~n") && echo=Numbers in this variable _str: !_l!
:: -----------------------------------------------------------------------------
:: loop output !_str! reults == 0 && set "_var=!_var!%%~n results == 0
:: loop output !_str! reults == 1 && set "_var=!_var!%%~n results == 01
:: loop output !_str! reults == 2 && set "_var=!_var!%%~n results == 012
:: loop output !_str! reults == 3 && set "_var=!_var!%%~n results == 0123
:: loop output !_str! reults == 4 && set "_var=!_var!%%~n results == 01234
:: loop output !_str! reults == 5 && set "_var=!_var!%%~n results == 012345
:: loop output !_str! reults == 6 && set "_var=!_var!%%~n results == 0123456
:: loop output !_str! reults == 7 && set "_var=!_var!%%~n results == 01234567
:: loop output !_str! reults == 8 && set "_var=!_var!%%~n results == 012345678
:: loop output !_str! reults == 9 && set "_var=!_var!%%~n results == 0123456789
:: -----------------------------------------------------------------------------
get all numbers 01234 from string independent of the delimiter
@echo off && setlocal EnableDelayedExpansion
(set "_str= Record Count/CC_NUMBER = 01234 "
for /f %%n in ('cmd /u /c echo="!_str!"^|find /v " "^|findstr [0-9]
')do set "_l=!_l!%%~n") && echo=Numbers in this variable _str: !_l!
rem :: Code Results: Numbers in this variable _str: 01234
To get all numbers from string in any position independent of the delimiter
- String:
0R/ecord\-# ',? 1>|<2Count>/3$4]C{^5_6&7}NUMBER = *|8!9
- Output:
Numbers in this variable _str: 0123456789
@echo off && setlocal EnableDelayedExpansion
(set "_str=0R/ecord\-# ',? 1>|<2Count>/3$4]C{^5_6&7}NUMBER = *|8!9"
for /f %%n in ('cmd /u /c echo="!_str!"^|find /v " "^|findstr [0-9]
')do set "_l=!_l!%%~n") && echo=Numbers in this variable _str: !_l!