使用批处理从文本文件中读取多行

时间:2017-01-24 17:10:48

标签: batch-file

我正在使用包含证书数据的文件

###################
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
###################

我正在寻找批处理脚本中的帮助来过滤掉

的值
  1. 签名有时间戳:
  2. SHA1哈希:
  3. 有人可以帮助我吗?

    我试过了,但不是很好:

    @echo off
    for /f "tokens=*" %%a in (result.txt) do (
      echo line=%%a
    )
    pause
    

    谢谢,

3 个答案:

答案 0 :(得分:4)

另一个选择

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "timestamped="
    set "hash="

    :: Token positions inside the lines:
    ::
    ::    1a  2b        3c 4d           5e   
    ::    The signature is timestamped: Wed Nov 30 18:19:59 2016
    ::        1a   2b    3c
    ::        SHA1 hash: BE36A4562FB2EE05DBB3D32323ADF445084ED656

    for /f "tokens=1-4,*" %%a in ('
        findstr /l 
            /c:"The signature is timestamped:"
            /c:"SHA1 hash:"
            "result.txt"
    ') do if "%%d"=="timestamped:" ( 
        set "timestamped=%%e" 
    ) else (
        set "hash=%%c"
    )

    echo timestamped %timestamped% 
    echo hash        %hash%

这会过滤文件(findstr)以仅从文件中检索所需的行。 for /f处理这些行检索请求的令牌。

答案 1 :(得分:1)

试试这个(未经测试):

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%

答案 2 :(得分:1)

以下脚本,我们称之为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 ('

...为:

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 /B