Output to text file
Moderator: DosItHelp
Output to text file
Hi,
I am trying to output results of a for loop to a text file. Here's the command
C:\>for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do echo %i >>tmp.txt
Easy enough... i get my line numbers matching the string to tmp.txt. The output in tmp.txt looks like
135
330
479
660
818
970
What I want instead is to add 1 number and display the output as
136
331
480
661
819
971
So tried this
for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do set /a n=%i+1 >>tmp.txt
but my output is 136331480661819971 in the tmp.txt file. Either of the following two are acceptable
space delimited 136 331 480 661 819 971 or
136
331
480
661
819
971
Plz help. Thanks a bunch in advance.
I am trying to output results of a for loop to a text file. Here's the command
C:\>for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do echo %i >>tmp.txt
Easy enough... i get my line numbers matching the string to tmp.txt. The output in tmp.txt looks like
135
330
479
660
818
970
What I want instead is to add 1 number and display the output as
136
331
480
661
819
971
So tried this
for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do set /a n=%i+1 >>tmp.txt
but my output is 136331480661819971 in the tmp.txt file. Either of the following two are acceptable
space delimited 136 331 480 661 819 971 or
136
331
480
661
819
971
Plz help. Thanks a bunch in advance.
Re: Output to text file
Hi A_Bobby,
this should work
The main problem was, that the output of the SET command is always without line end (CR/LF).
hope it helps
jeb
this should work
Code: Select all
for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do ( set /a n=%i+1 & echo( ) >>tmp.txt
The main problem was, that the output of the SET command is always without line end (CR/LF).
hope it helps
jeb
Re: Output to text file
Thanks for that Jeb. It worked. I am also trying to output the resulting line content to a text file. Is that possible with a single for loop?
Re: Output to text file
Hmmm ?
You redirect it to a file (tmp.txt), so I don't understand what your question is?
You redirect it to a file (tmp.txt), so I don't understand what your question is?
Re: Output to text file
My bad!
Here's my issue. I am trying to capture multiple lines from an output of a command. The command is certutil -v -store My. Basically its dumping the entire certificate store information and what I am trying to capture are SKID (subject key identifiers) values. The skid values are represented by string "_ID(20)" but the skid value itself is on the next line, hence set /a n=%i+1. Below is part of the the output that I am interested in.
CERT_KEY_IDENTIFIER_PROP_ID(20): -Line number 135
a0 18 d2 cd c5 c3 ce ac be ae 78 dd 02 58 ef 65 c9 f7 5a 72 -Line number 136
What I am interested in is finding and isolating the string _ID(20):, adding one to that variable and use that variable to dump that line to another output file using best way possible. So I was first trying to create a tmp.txt file with line numbers and then using that to dump corresponding lines to another text file.
Hope this clarifies it. Thanks again for looking at it.
Here's my issue. I am trying to capture multiple lines from an output of a command. The command is certutil -v -store My. Basically its dumping the entire certificate store information and what I am trying to capture are SKID (subject key identifiers) values. The skid values are represented by string "_ID(20)" but the skid value itself is on the next line, hence set /a n=%i+1. Below is part of the the output that I am interested in.
CERT_KEY_IDENTIFIER_PROP_ID(20): -Line number 135
a0 18 d2 cd c5 c3 ce ac be ae 78 dd 02 58 ef 65 c9 f7 5a 72 -Line number 136
What I am interested in is finding and isolating the string _ID(20):, adding one to that variable and use that variable to dump that line to another output file using best way possible. So I was first trying to create a tmp.txt file with line numbers and then using that to dump corresponding lines to another text file.
Hope this clarifies it. Thanks again for looking at it.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Output to text file
get a good file parsing tool, like gawk for windows.
Code: Select all
C:\test> cert -v ..... | gawk "/CERT.*_ID\(20\)/{getline;print}"
Re: Output to text file
Using gawk is not an option. I made some progress and I am able to get the lines using the following script
First I dump contents of cert stores in dump file with line numbers using this command
certutil -v -store My | findstr /v /n 1362322 >>dump.txt
Then I create a temp file with the line numbers that I am interested in using the following command that you guys fixed for me
for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do ( set /a n=%i+1 & echo( ) >>tmp.txt
Then I use this command to output the lines that I need
for /f %i in (tmp.txt) do findstr %i: dump.txt
It gives me the output like following
C:\>findstr 136: dump.txt
136: 5a 4b fb 38 d3 e9 b3 17 06 04 be 73 5a b9 f0 69 76 61 00 19
C:\>findstr 331: dump.txt
331: d7 9b 7c d8 22 a0 15 f7 dd ad 5f ce 29 9b 58 c3 bc 46 00 b5
C:\>findstr 480: dump.txt
480: 76 8e fd e4 e3 e7 a3 fe eb 6a 0a 95 a3 6d 2a 74 d0 79 31 06
What is unwanted in this output is the line numbers at the beginning and spaces inbetween characters. I would like to get the output like
C:\>findstr 136: dump.txt
5a4bfb38d3e9b3170604be735ab9f06976610019
C:\>findstr 331: dump.txt
d79b7cd822a015f7ddad5fce299b58c3bc4600b5
C:\>findstr 480: dump.txt
768efde4e3e7a3feeb6a0a95a36d2a74d0793106
Is it possible to do that?
First I dump contents of cert stores in dump file with line numbers using this command
certutil -v -store My | findstr /v /n 1362322 >>dump.txt
Then I create a temp file with the line numbers that I am interested in using the following command that you guys fixed for me
for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do ( set /a n=%i+1 & echo( ) >>tmp.txt
Then I use this command to output the lines that I need
for /f %i in (tmp.txt) do findstr %i: dump.txt
It gives me the output like following
C:\>findstr 136: dump.txt
136: 5a 4b fb 38 d3 e9 b3 17 06 04 be 73 5a b9 f0 69 76 61 00 19
C:\>findstr 331: dump.txt
331: d7 9b 7c d8 22 a0 15 f7 dd ad 5f ce 29 9b 58 c3 bc 46 00 b5
C:\>findstr 480: dump.txt
480: 76 8e fd e4 e3 e7 a3 fe eb 6a 0a 95 a3 6d 2a 74 d0 79 31 06
What is unwanted in this output is the line numbers at the beginning and spaces inbetween characters. I would like to get the output like
C:\>findstr 136: dump.txt
5a4bfb38d3e9b3170604be735ab9f06976610019
C:\>findstr 331: dump.txt
d79b7cd822a015f7ddad5fce299b58c3bc4600b5
C:\>findstr 480: dump.txt
768efde4e3e7a3feeb6a0a95a36d2a74d0793106
Is it possible to do that?
Re: Output to text file
You could put it into a batch file
myFind.bat
myFind 331 dump.txt
myFind.bat
Code: Select all
@echo off
for /F "usebackq delims=" %%a in (`findstr %1 %2`) DO (
set "line=%%a"
)
REM ** remove the line number, all characters to the first ":"
set "line=%line:*:=%"
REM ** remove all spaces
set "line=%line: =%"
echo %line%
myFind 331 dump.txt
Re: Output to text file
Thanks Jeb. This works but I have run into another problem. I am trying to put everything into one batch file but the following command
for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do ( set /a n=%i+1 & echo( ) >>tmp.txt
that you fixed for me is dumping blank echo's into the tmp.txt file. It works fine when I run it from command line but doesn't work within batch file.
for /f "delims=:" %i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do ( set /a n=%i+1 & echo( ) >>tmp.txt
that you fixed for me is dumping blank echo's into the tmp.txt file. It works fine when I run it from command line but doesn't work within batch file.
-
- Posts: 79
- Joined: 13 Dec 2010 10:32
Re: Output to text file
In batch file, you need %%i in for loops. So:
Code: Select all
for /f "delims=:" %%i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do ( set /a n=%%i+1 & echo( ) >>tmp.txt
Re: Output to text file
I know that but I am getting the blank output even with %%. If I don't use double %, the error is different... %i was unexpected at this time.
Re: Output to text file
Hello Experts,
Any update on if this is possible? Please reply regardless.
Thanks
Any update on if this is possible? Please reply regardless.
Thanks
Re: Output to text file
Ok it is tricky.
On the command line the set /a echo's also the result, but in the batch file it is quite.
So you have to output explicit.
hope it helps
jeb
On the command line the set /a echo's also the result, but in the batch file it is quite.
So you have to output explicit.
Code: Select all
setlocal EnableDelayedExpansion
for /f "delims=:" %%i in ('certutil -v -store My ^| findstr /i /n "_ID(20)"') do (
set /a n=%%i+1
echo !n!
) >>tmp.txt
hope it helps
jeb
Re: Output to text file
Great! That worked. Thanks a lot Jeb. This little script will save me a at least a 100 man hours!