1

I have file.txt with a line that is just like this...

{ "success" : { "id" : "18" } }

How can I ignore all of that, and get just the number (18) and store it in a variable so I can use it?

Note, the number may change, it won't always be 18, and could be 1 to 3 digits.

I've been playing around with for but can't seem to get the tokens right cause the number is surrounded by quotes

for /F "tokens=*" %%A in file.txt) do (

)
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Lego Man
  • 13
  • 2
  • Please see this answer as i feel it is the same: http://stackoverflow.com/questions/19356220/how-to-parse-a-json-file-to-variables-with-the-windows-command-line – NormTheThird Nov 08 '16 at 04:08

2 Answers2

0
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q40478716.txt"
FOR /f "usebackqtokens=4delims=:{}" %%a IN ("%filename1%") DO SET /a var=%%~a
ECHO result=%var%
GOTO :EOF

You would need to change the setting of sourcedir to suit your circumstances.

I used a file named q40478716.txt containing your data for my testing.

The 4th token is assigned to %%a and the quotes removed by the ~

Magoo
  • 77,302
  • 8
  • 62
  • 84
-2
import re
number = re.findall(r'\d+', 'target_str')[0]
grantonzhuang
  • 557
  • 4
  • 6