批处理解析命令输出

时间:2015-07-13 09:25:52

标签: batch-file mobile cmd imei

我有以下命令和输出:

    d:\adb>adb shell service call iphonesubinfo 1
Result: Parcel(
  0x00000000: 00000000 0000000f 00350033 00380039 '........8.5.9.8.'
  0x00000010: 00320037 00360030 00350032 00310034 '7.2.0.1.2.4.4.1.'
  0x00000020: 00390039 00000039                   '5.9.9...        ')

我如何解析它并只在一行中获得IMEI?

还:

< waiting for device >
...
(bootloader) ---------------------------------------------------------
(bootloader) Device-ID
(bootloader) 2FC9A68923FD175AA6E13657181CA6AB
(bootloader) 4AE438F12376AFA85D0E3467AE83A752
(bootloader) ---------------------------------------------------------
OKAY [  0.020s]
finished. total time: 0.020s

获取序列号为:2FC9A68923FD175AA6E13657181CA6AB4AE438F12376AFA85D0E3467AE83A752

1 个答案:

答案 0 :(得分:3)

请在“代码”中注明我的REM评论

@echo off
REM Spliting the IMEI multiline output to lines
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`adb shell service call iphonesubinfo 1`) DO (
  SET var!count!=%%F
  SET /a count=!count!+1
)

REM From each output line of interest we take the IMEI "string" portion 
for /f "tokens=6" %%G IN ("%var2%") DO SET v1=%%G
for /f "tokens=6" %%G IN ("%var3%") DO SET v2=%%G
for /f "tokens=4" %%G IN ("%var4%") DO SET v3=%%G

REM Stiching the IMEI parts to one string
SET imei=%v1%%v2%%v3%
SET imei=%imei:'=%
SET imei=%imei:.=%
ECHO %imei%

ENDLOCAL