0

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.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
alidrsn
  • 109
  • 1
  • 10

6 Answers6

1

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
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Another possible way (for this special usecase): `for /f "tokens=2 delims==" %%i in ("%line: =%") do echo %%i` – Stephan Nov 01 '19 at 10:18
  • Thanks for the suggestions guys. You made my day :) – alidrsn Nov 01 '19 at 11:01
  • 1
    @alidrsn. No problem, so what you need to do is consider which of these answers solved your problem then mark it as the correct answer. – Gerhard Nov 01 '19 at 11:13
1

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: *, ?, <, >, ", ^, |, &, (, ).

aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

If the desired token is strict numerical (within the bounds of INT32), you can use set /a, which ignores any spaces:

@echo off
set "line=this can be any length Record Count/CC_NUMBER = 123"
for /f "tokens=2 delims==" %%i in ("%line%") do set /a var=%%i
echo "%var%"
Stephan
  • 53,940
  • 10
  • 58
  • 91
0

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
::----------------------------------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70
0

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.


  • Where all characters turn output like this: U N I C O D E
  • Unicode output layout is mainly characterized by the space between them in cmd /u echo your string.
  • When taking the output characters one by one, you don't have to worry/working with the delimiter's, the 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 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 
:: -----------------------------------------------------------------------------

enter image description here


  • 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

enter image description here



  • 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!


enter image description here


Io-oI
  • 2,514
  • 3
  • 22
  • 29