1

i am having following file with certificate data

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

I am looking for help in batch script to filter out values of

  1. The signature is timestamped:
  2. SHA1 hash:

Can anyone please help me ?

I tried following, but not good:

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

Thanks,

3 Answers 3

4

Another option

@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%

This filters the file (findstr) to only retrieve the needed lines from the file. The for /f processes those lines retrieving the requested tokens.

Sign up to request clarification or add additional context in comments.

Comments

1

try this (not tested):

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%

1 Comment

The space following the colons will be part of the var contents.So echo:"%timestamped:~1%" -- "%hash:~1%" should account for that.
1

The following script, let us call it extract.bat, does what you are asking for. It regards only lines that lie in between the first and second line containing ###################:

@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

Provide your text file as a command line argument, like:

extract.bat "result.txt"

If you want to process the data in between the second and third line containing ###################, change the line...:

for /F "delims=:" %%K in ('

...to:

for /F "skip=1 delims=:" %%K in ('

Increment the skip number accordingly for processing the next block.


If you do not care about the ################### lines (in case your text file does not contain multiple blocks of data), the script can be simplified to this:

@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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.