Finding unknown string between two strings in a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rahul
Posts: 7
Joined: 07 Aug 2010 01:18

Finding unknown string between two strings in a file

#1 Post by rahul » 07 Aug 2010 02:34

I am new to batch programming :roll: :roll:

could you please specify commands to identify the unknown string in between known two strings
1)Execute a command output to search string in between known string
2)Need to use identified string for other next operations

For the above 1st operation used for loop to execute the command only & unable to search string

For the 2nd operation Unable to find search . :cry:

Example:

a)String to be identify is dynamic or not fixed line numbers
b)Below is the command output to identify string
c)known string in the below file is: 1)Name: 2)Driver is running.
d)I want to use the string which is between above between string. i,e

Name:we needed string in between lines #6
Driver is running.

below is the command out :
---------------------------------------------------------------------------------------
USB\VID_111f&PID_0203&MI_00\6&25A2C8F4&5&0000
Name:we needed string in between lines #2
USB\VID_111f&PID_0203&MI_00\6&25A2C8F4&6&0000
Name:we needed string in between lines #5
USB\VID_111f&PID_0203&MI_00\6&25A2C8F4&7&0000
Name:we needed string in between lines #6
Driver is running.
USB\VID_111f&PID_0203&MI_00\6&B3883FF&1&0000
Name:we needed string in between lines #4
4 matching device(s) found.
----------------------------------------------------------------------------------------

Please help me....experts :idea:

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Finding unknown string between two strings in a file

#2 Post by !k » 07 Aug 2010 03:46

Code: Select all

@echo off

set "output=c:\output.txt"

for /f "delims=:" %%n in ('findstr /x /n /c:"Driver is running." %output%') do set /a number=%%n-1
for /f "tokens=2,* delims=:" %%s in ('findstr /b /n /c:"Name:" %output% ^|findstr /b /c:"%number%:Name:"') do set "string=%%t"
echo line # is %number%
echo string is "%string%"
pause


rahul, delete duplicate thread

rahul
Posts: 7
Joined: 07 Aug 2010 01:18

Re: Finding unknown string between two strings in a file

#3 Post by rahul » 07 Aug 2010 04:34

Thanks !K ,,but it was not working .. i tweaked the code but still not working :cry:

I saved the example file and code into into test.cmd.. but stilll not working

Below was the output of executation...

C:\>test.cmd
line # is
string is ""
Press any key to continue . . .

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Finding unknown string between two strings in a file

#4 Post by !k » 07 Aug 2010 05:23

The batch is tested on a sample output from the first post.
Give the real output file, preferably as (.ZIP +) .UUE in [code] tag

rahul
Posts: 7
Joined: 07 Aug 2010 01:18

Re: Finding unknown string between two strings in a file

#5 Post by rahul » 07 Aug 2010 06:55

Hi The Out file is the same real output file which as i pasted previously.....

Please help in to obtain the output..

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Finding unknown string between two strings in a file

#6 Post by !k » 07 Aug 2010 20:33

Image
Probably the real strings have spaces at the end. Modify the code so
for /f "delims=:" %%n in ('findstr /b /n /c:"Driver is running." %output%') do set /a number=%%n-1

rahul
Posts: 7
Joined: 07 Aug 2010 01:18

Re: Finding unknown string between two strings in a file

#7 Post by rahul » 08 Aug 2010 01:33

Thanks !k, I changed as per instruction now i am able to ger the line number and not the string. Output as below
----------------------------------------------------------------------------------------------------------------
C:\>test.cmd

C:\>set "output=c:\dev.txt"

C:\>for /F "delims=:" %n in ('findstr /b /n /c:"Driver is running." c:\dev.txt')
do set /a number=%n-1

C:\>for /F "tokens=2,* delims=:" %s in ('findstr /b /n /c:"Name:" c:\dev.txt |fi
ndstr /b /c:"6:Name:"') do set "string=%t"

C:\>echo line # is 6
line # is 6

C:\>echo string is ""
string is ""
------------------------------------------------------------------------------------------------------------
My out put file contains the below real format strings and symbols.

Name: Wireless 1111 (VE-OD-APSH) Mobilephone Broadcast Card-Mini AEMN (COM23)
Driver is running.

Name: Wireless 1111 (VE-OD-APSH) Mobilephone Broadcast Card-Mini Circuit Adaptor #6
Driver is running.


---------------------------------------------------------------------------------------------------------------

Please help me out of the issue !K :?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Finding unknown string between two strings in a file

#8 Post by !k » 09 Aug 2010 11:31

May be forum not transmit any special characters in your output file.
Find a way to pass the output file unchanged (archive, UUEncode)

rahul
Posts: 7
Joined: 07 Aug 2010 01:18

Re: Finding unknown string between two strings in a file

#9 Post by rahul » 11 Aug 2010 02:56

Could you let us know what you are expected from previous reply, I am unable to get you what your requesting..
:(

1) I would like to use a string which exist at n th line number of data file(file containing lines of words) .
2)And to parse the exact data what i need for further operations( ":" symbol to separate left and right data to use

Example:
File data as below:
-----------------------------FILE DATA_START-----------------------------
aASA
ASAS
ASAS
Name: Wireless 1111 (VE-OD-APSH) Mobilephone Broadcast Card-Mini AEMN (COM23)
ASASAS
ASASSSA
ASASSA

-----------------------------FILE DATA_ENd-----------------------------

1>In the above I need fourth line data .. Let us say if u say line number 4, i need to get that data
2>In the current line I need to split the data by symbol ":" , Left side and right side, which i can use for further operations. :idea:

Post Reply