Search code examples
batch-filesubstringadb

batch file: Parse string after substring occurence, not delimeter


I have and batch file script shows me output of adb utility.

FOR /F "skip=1 delims=~" %%x IN ('adb devices -l') DO echo %%x

2 output lines

0123456789ABCDEF       device product:java_joyplus_qb7 model:TM702B4_3G device:java_joyplus_qb7 transport_id:12
F9NPFP084096           device product:WW_P023 model:P023 device:P023_1 transport_id:11

I want parse non-space substring after "transport_id:". For the first line i want get 12, for second 11. How can I do that?


Solution

  • If I understand correctly what you've asked, the following should output the information you require.

    @Echo Off
    
    For /F "Skip=1 Tokens=1*" %%G In ('adb.exe devices -l') Do (Set "DI=%%H"
        SetLocal EnableDelayedExpansion
        For /F %%I In ("!DI:*transport_id:=!") Do EndLocal & Echo %%I)
    
    Pause
    

    And, if you wanted to do that from :

    For /F "Skip=1Tokens=1*" %G In ('adb.exe devices -l')Do @Set "DI=%H"&For /F %I In ('Cmd /D/V/C Echo "!DI:*transport_id:=!"')Do @Echo %~I