user9324026
user9324026

Reputation:

Batch file - extract specific text from .txt file

I need to pull out some text from file using cmd text file simplified example

{"code1":"7adu627","code2":"jwfuj4r","code3":"dsfhy3","code4":"usgf634"}

i need to pull out text that comes after code2 and code 3 so output will be this

code2: jwfuj4r
code3: dsfhy3

or just this

jwfuj4r
dsfhy3

i found other posts like this but they didn't work or i couldn't get it to work.

Upvotes: 1

Views: 2538

Answers (1)

Stephan
Stephan

Reputation: 56238

@echo off
for /f "tokens=2,3 delims=," %%a in (file.txt) do (
  for /f "tokens=2 delims=:" %%c in ("%%a") do echo %%~c
  for /f "tokens=2 delims=:" %%c in ("%%b") do echo %%~c
)

Note: this works with your example. Any solution will highly depend on your real data.

A bit more generic (search explicitely for code2 and code3):

@echo off
for /f "delims=" %%a in (file.txt) do (
  for %%b in (%%a) do (
    for /f "tokens=1,2 delims=:" %%c in ("%%b") do (
      echo %%c|findstr "code2 code3">nul && echo %%~d
    )
  )
)

Upvotes: 2

Related Questions