Extracting a sub-string from a FINDSTR command


A

Alex Horan

Hello,

I have searched for this on line but have not had any luck.

I have a specific issue that I am trying to find a solution to. I
currently use the FINDSTR command to bring back some information from a
file. However I am getting too much information and would like to then
extract a string from within the output returned.

The output returned is shown:

FINDSTR NAME file.exe
....info{\title NAME: Company - Alex Horan}{\author ...

The information I want is: 'Company - Alex Horan' - the information
always comes after NAME: and then finishes at the '}'

I need some method to count where in the string returned the NAME: ends
and the when the next '}' is - all the characters within that string
will be the information I want. I then need to extract that string out.

Is there anyway to do this in a batch file? If someone could point me in
the right direction I would really appreciate it!

Alex Horan
 
Ad

Advertisements

M

Matthias Tacke

Alex said:
Hello,

I have searched for this on line but have not had any luck.

I have a specific issue that I am trying to find a solution to. I
currently use the FINDSTR command to bring back some information from a
file. However I am getting too much information and would like to then
extract a string from within the output returned.

The output returned is shown:

FINDSTR NAME file.exe
...info{\title NAME: Company - Alex Horan}{\author ...

The information I want is: 'Company - Alex Horan' - the information
always comes after NAME: and then finishes at the '}'

I need some method to count where in the string returned the NAME: ends
and the when the next '}' is - all the characters within that string
will be the information I want. I then need to extract that string out.

Is there anyway to do this in a batch file? If someone could point me in
the right direction I would really appreciate it!

Alex Horan
To my experience findstr can't deal well with possible binary data.
Your example output is a bit small.

I'd use strings to first extract strings with a minimum length from the
file and then use frindstr to get a matching line. Then use for to parse
the output. This depends on unique identifiers in the line.

http://www.sysinternals.com/ntw2k/source/misc.shtml#strings

You might experiment with this sample batch.

@echo off&setlocal
for /f "tokens=1-3 delims=[] " %%A in (
'strings -n 10 "%systemroot%\system32\findstr.exe"^|findstr "["'
) do echo.%%A %%B %%C


HTH
 
P

Paul R. Sadowski [MVP]

Hello, Alex!
You wrote on Mon, 01 Nov 2004 14:57:26 -0500:

AH> I have searched for this on line but have not had any luck.

AH> I have a specific issue that I am trying to find a solution to. I
AH> currently use the FINDSTR command to bring back some information from a
AH> file. However I am getting too much information and would like to then
AH> extract a string from within the output returned.

AH> The output returned is shown:

AH> FINDSTR NAME file.exe
AH> ...info{\title NAME: Company - Alex Horan}{\author ...

AH> The information I want is: 'Company - Alex Horan' - the information
AH> always comes after NAME: and then finishes at the '}'

Well, quickly, if there are no other curly braces in the string then this
should work:
@echo off & setlocal
set TheString=...info{\title NAME: Company - Alex Horan}{\author ...
for /f "tokens=1,2* delims={}" %%c in ("%TheString%") do (
for /f "tokens=2* delims=:" %%e in ("%%d") do set TheString=%%e
)
echo %TheString:~1%

With best regards, Paul R. Sadowski [MVP]. E-mail: (e-mail address removed)
 
A

Alex Horan

Thank you both for your answers. Both ideas work but I am going to use
Paul's answer. I want to be able to easily pass this batch file around
different users with out having to email additional files.

Again - thank you both for your prompt assistance.
 
Ad

Advertisements

M

Matthias Tacke

Alex said:
Thank you both for your answers. Both ideas work but I am going to use
Paul's answer. I want to be able to easily pass this batch file around
different users with out having to email additional files.

Again - thank you both for your prompt assistance.
Fine if a solution works for you.
I had negative experience with findstr and binary data occuring in
*.exe files which your example showed.
Seems Paul had a better crystal ball to see your data :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Findstr and multi-string AND searches 12
findstr, for, or other command to do this? 8
Findstr and Bmail 1
extracting info from string 2
findstr 9
Findstr question 8
findstr question 2
Findstr, problem with $ anchor 4

Top