比如,将第1条提取出所有数字,如果前一个词不是"dsubsys:",然后存在result1变量里,第二条存在result2变量里,以此类推,你再自行处理输出.不过文件比较大时,批处理会非常慢,建议用C语言等其他方法.测试结果:
第1条:0001d554bc2102022078AC8566e500611103fb4f2102551046424d
第2条:0001d554bc2102022078AC8566e500611103fb4f2102551046424d
@echo off
setlocal enabledelayedexpansion
call:main
endlocal
echo.
pause>con
goto :eof
:main
set /a num=0
set "lastword="
for /f "delims=" %%i in ('type "message.txt"') do (
set "line=%%i"
call:add
REM 如果词数可能很多,那么要用goto,这样此处必须使用call,否则goto时会退出循环
)
REM 处理完毕
(
for /l %%i in (1,1,!num!) do (
set/p
REM 输出换行号
)
)>con
REM 输出到文件con(屏幕)
exit /b
:add
call:getword "line" ". "
REM 从该行中读取一个词,以". "分隔
if errorlevel 1 goto :eof
REM 如果读到结尾,返回for循环以分析新的一行
if "!answer!"=="flag" (
set /a num+=1
set "result!num!="
)
REM 如果读到的词是flag,条数加一
call:isnum answer
if errorlevel 1 if not "!last!"=="dsubsys:" (
for %%i in (!num!) do set "result%%i=!result%%i!!answer!"
)
REM 如果读到的词是16进制数,而且上一个词不是dsubsys:,存到条数对应的变量里
set "last=!answer!"
goto add
:getword
set "answer="
for /f "tokens=1* delims=%~2" %%i in ("!%~1!") do (
set "answer=%%i"
set "%~1=%%j"
)
if "!answer!"=="" (
set "%~1="
exit /b 1
)
exit /b 0
:isnum
REM 假设 "0001" "d5" 这样的数字最长为32位
for /l %%i in (0,1,31) do (
if "!%~1:~%%i,1!"=="" exit /b 1
set /a i=0
for %%j in (1 2 3 4 5 6 7 8 9 0 a b c d e f) do (
if /i "!%~1:~%%i,1!"=="%%j" set /a i=1
)
if "!i!"=="0" exit /b 0
)
echo 太长了:!%~1!
exit /b 0
去掉点的比较简单,我就不写了.下面这个直接从原文(每行,以空格,点为分隔)提取16进制数字.核心是getword函数.
测试结果:0001d554bc2102fa022078AC8566e50061110305fb4f2102551046424d
@echo off
setlocal enabledelayedexpansion
call:main
endlocal
echo.
pause>con
goto :eof
:main
set "result="
for /f "delims=" %%i in ('type "message.txt"') do (
set "line=%%i"
call:add
)
echo !result!
exit /b
:add
call:getword "line" ". "
if errorlevel 1 goto :eof
call:isnum answer
if errorlevel 1 set "result=!result!!answer!"
goto add
:getword
set "answer="
for /f "tokens=1* delims=%~2" %%i in ("!%~1!") do (
set "answer=%%i"
set "%~1=%%j"
)
if "!answer!"=="" (
set "%~1="
exit /b 1
)
exit /b 0
:isnum
REM 假设 "0001" "d5" 这样的数字最长为32位
for /l %%i in (0,1,31) do (
if "!%~1:~%%i,1!"=="" exit /b 1
set /a i=0
for %%j in (1 2 3 4 5 6 7 8 9 0 a b c d e f) do (
if /i "!%~1:~%%i,1!"=="%%j" set /a i=1
)
if "!i!"=="0" exit /b 0
)
echo 太长了:!%~1!
exit /b 0
下面这个是优化过的,快一些.
@echo off
setlocal enabledelayedexpansion
set "result="
for /f "delims=" %%i in ('type "message.txt"') do (
set "line=%%i"
call:add
)
echo !result!
pause
goto :eof
:add
set "answer="
for /f "tokens=1* delims=. " %%i in ("!line!") do (
set "answer=%%i"
set "line=%%j"
)
if "!answer!"=="" goto :eof
set /a is=2
for /l %%i in (0,1,31) do (
if "!answer:~%%i,1!"=="" (
set "result=!result!!answer!"
goto add
)
set /a i=0
for %%j in (1 2 3 4 5 6 7 8 9 0 a b c d e f) do (
if /i "!answer:~%%i,1!"=="%%j" set /a i=1
)
if "!i!"=="0" goto add
)
echo 太长了:!answer!
goto add
楼主,如此看来你的内容并无什么规律性,如果这样要来批处理解决的法,显然很是困难...
不过你说的.号如何去掉.我做个例子
for /f "delims=" %%a in (a.txt) do (
set "jg=%%a"
call,set "jg=%%jg:.=%%"
call echo %%jg%%
)
pause
那A.TXT里面的内容有.这个符号都会被替换为空
set %test:.=%
echo %test%
十六进制数没有可供提取的标识
所以如果格式都一样的话比较方便的是按列提取
下面这个命令提取message.txt中每一行的第2,4,6列的值,合并起来并在前面加零,输出到new.txt
for /f "tokens=2,4,6" %%a in (message.txt) do echo 00%%a%%b%%c>>new.txt
应该是有大量同样格式的记录要提取吧,那你就手动指定每一条记录要提取的行数,然后批量重复,这样也是可以接受的
for /f %%a in (message.txt) do
这条语句的基本语法就是,读取message.txt中的内容,每读一行,就把这一行的内容赋给%%a,并且运行do后面的语句段