我有以下带有证书数据的文件
###################
The signature is timestamped: Wed Nov 30 18:19:59 2016
Timestamp Verified by:
Issued to: Thawte Timestamping CA
Issued by: Thawte Timestamping CA
Expires: Fri Jan 01 05:29:59 2021
SHA1 hash: BE36A4562FB2EE05DBB3D32323ADF445084ED656
###################我正在寻找批处理脚本中的帮助,以筛选出
有人能帮帮我吗?
我试过跟随,但不好:
@echo off
for /f "tokens=*" %%a in (result.txt) do (
echo line=%%a
)
pause谢谢,
发布于 2017-01-24 17:19:43
试试这个(没有测试):
for /f "skip=1 tokens=1* delims=:" %%a in ('find "The signature is timestamped:" "result.txt" ') do (
set "timestamped=%%b"
)
for /f "skip=1 tokens=1* delims=:" %%a in ('find "SHA1 hash" "result.txt" ') do (
set "hash=%%b"
)
echo %timestamped% -- %hash%发布于 2017-01-24 18:02:18
下面的脚本,让我们称之为extract.bat,做您想要的。它只考虑位于包含###################的第一行和第二行之间的行。
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FRAME=###################"
set "_FILE=%~1" & rem // (use file provided by command line argument)
set "_SEARCH[1]=The signature is timestamped:"
set "_SEARCH[2]=SHA1 hash:"
rem "_SEARCH[...]=...:"
rem // Determine numbers of first and second lines containing string `%_FRAME%`:
set "FRST=" & set "SCND="
for /F "delims=:" %%K in ('
findstr /N /C:"%_FRAME%" "%_FILE%"
') do (
if not defined FRST (
set /A "FRST=%%K"
) else (
if not defined SCND set /A "SCND=%%K"
)
)
rem // Continue only if two lines have been found containing string `%_FRAME%`:
if defined FRST if defined SCND (
rem // Enumerate all search strings `_SEARCH[?]` in alphabetical order:
for /F "tokens=1,* delims==" %%I in ('2^> nul set _SEARCH[') do (
rem // Process currently iterated search string:
for /F "tokens=1,2,* delims=:" %%L in ('
findstr /N /C:"%%J" "%_FILE%"
') do (
rem // Check if current line lies in between the two derived before:
if %%L GTR %FRST% if %%L LSS %SCND% (
rem // Return remaining string value with leading spaces removed:
for /F "tokens=*" %%O in ("%%N") do echo(%%O
)
)
)
)
endlocal
exit /B将文本文件作为命令行参数提供,如:
extract.bat "result.txt"如果要在包含###################的第二行和第三行之间处理数据,请更改该行.:
for /F "delims=:" %%K in ('...to:
for /F "skip=1 delims=:" %%K in ('相应地增加用于处理下一个块的skip号。
如果您不关心###################行(如果文本文件不包含多个数据块),可以将脚本简化为:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (use file provided by command line argument)
set "_SEARCH[1]=The signature is timestamped:"
set "_SEARCH[2]=SHA1 hash:"
rem "_SEARCH[...]=...:"
rem // Enumerate all search strings `_SEARCH[?]` in alphabetical order:
for /F "tokens=1,* delims==" %%I in ('2^> nul set _SEARCH[') do (
rem // Process currently iterated search string:
for /F "tokens=1,* delims=:" %%M in ('
findstr /C:"%%J" "%_FILE%"
') do (
rem // Return remaining string value with leading spaces removed:
for /F "tokens=*" %%O in ("%%N") do echo(%%O
)
)
endlocal
exit /Bhttps://stackoverflow.com/questions/41834246
复制相似问题