Solved

FINDSTR Multiple occurances of text in order

Posted on 2010-09-07
15
524 Views
Last Modified: 2012-05-10
I want FINDSTR to search for two text occurances in order.

ex. John Smith
      12345689
       78945219
       Jane Doe
       48784543
       Bob Dole
       44787878
       45787845
       45787864

I tried the below syntax in the batch file;


type 837b.txt | findstr "NM1.QD" "REF.D9" >> ClaimNo.txt

type 837b.txt | findstr "NM1.QD" AND "REF.D9" >> ClaimNo.txt

type 837b.txt | findstr "NM1.QD"  | findstr  "REF.D9" >> ClaimNo.txt


FYI - NM1*QD precedes the name and REF*D9 preceded the ClaimNo

None work in combination. Will also appreciate vbs solution.

Thanks

0
Comment
Question by:tonydemarco
  • 6
  • 5
  • 4
15 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33621478
Not completely sure what you want here - what is NM1.QD and REF.D9?  Are you wanting it to find all the lines with the name and then all the lines with the claim no. ,e .g.

type 837b.txt | findstr "NM1.QD" > ClaimNo.txt
type 837b.txt | findstr "REF.D9" >> ClaimNo.txt

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33621485
Please show us an example of the actual (amended if needed) data for a few lines, what you want to search and the required results
0
 
LVL 9

Author Comment

by:tonydemarco
ID: 33621559

re: "what is NM1.QD and REF.D9?"

FYI - NM1*QD precedes the name and REF*D9 preceded the ClaimNo in the file.

Findstr will not allow an asterisk so you must use a "." in its place.

I can not supply data file.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 33621624
I just mean one or two example lines with made up data in of course otherwise it is difficult to understand what you want to search for etc.
0
 
LVL 9

Author Comment

by:tonydemarco
ID: 33621770
OK here you go:

NM1*QD*1*JOHN*SMITH*B
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
REF*D9*0110286425537
REF*D9*0110286425538
NM1*QD*1*JANE*DOE*H
XXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX
REF*D9*0110286425539
NM1*QD*1*BOB*DOLE*R
REF*D9*0110286425540
REF*D9*0110286425541
REF*D9*0110286425542
REF*D9*0110286425543

OutPut should be:
ex. JOHN*SMITH*B
      0110286425537
      0110286425538
       JANE*DOE*H
       48784543
       BOB*DOLE*R
       0110286425540
       0110286425541
       0110286425542
       0110286425543
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33622008
Here you are this will create an output file exactly as you have requested. :-)

~Q

FOR /F "Tokens=1-3,* Delims=^*" %%A IN ('Type "C:\test\SourceFile.txt"') DO IF "%%A%%B"=="NM1QD" (ECHO %%D >> "C:\test\OutputFile.txt") ELSE (IF "%%A%%B"=="REFD9" ECHO %%C >> "C:\test\OutputFile.txt")

Open in new window

0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 33622029
Ok in which case you are going to have to parse it with a FOR loop then I guess I think, e.g.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET source="c:\source.txt"
FOR /f "tokens=1,2,3* delims=*" %%a in ('type %source%') do (
  if "%%a*%%b"=="NM1*QD" echo %%d
  if "%%a*%%b"=="REF*D9" echo %%c
)


returns for me:

[ C:\]> claims.cmd
JOHN*SMITH*B
0110286425537
0110286425538
JANE*DOE*H
0110286425539
BOB*DOLE*R
0110286425540
0110286425541
0110286425542
0110286425543


It takes each line in turn, splits them up into tokens at each * character returning into %%a, %%b, %%c, %%d.  %%d gets the fourth and remaining part of the line.
If the first two parameters are NM1*QD then it returns the fourth parmeter and rest of line otherwise the third.

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33622043
I should have said that displays to screen, can soon be directed into a file in one go by adding >output.txt after the final )

Steve
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33622056
If you woudl like for that to be a bit easier to edit here it is with a couple variables for you to specify the input and output files =)



SET "SourceFile=C:\test\SourceFile.txt"
SET "OutputFile=C:\test\OutputFile.txt"
FOR /F "Tokens=1-3,* Delims=^*" %%A IN ('Type "%SourceFile%"') DO IF "%%A%%B"=="NM1QD" (ECHO %%D >> "%OutputFile%") ELSE (IF "%%A%%B"=="REFD9" ECHO %%C >> "%OutputFile%")

Open in new window

0
 
LVL 9

Author Closing Comment

by:tonydemarco
ID: 33622170
Spot On!
0
 
LVL 9

Author Comment

by:tonydemarco
ID: 33622223
QCubed,

Thanks for your response.
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33622297
but my code does the same thing, and was posted sooner and I didn't even get an assist =( wha-wha "I lose"

heh, and also, you're welcome? ^^
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33623092
Qcubed, sorry I didn't even see your until after, we clearly were writing at the same time.

Steve
0
 
LVL 11

Expert Comment

by:Ben Personick
ID: 33623213
Lol, yeah, I posted the first code only a couple minutes before you posted yours, and there are only so many ways to skin a cat, so please don't think I'm saying that your code is at all derivative of mine.  I really respect your work on these forums!

But I admit I did feel a little gyped the author didn't give me an assist with minor points since I did post the sol first, but I know he was working with you, and you did post the solution to only a minute later, so you deserve the answer for sure.

  And The author probably doesn't even realize the similarity of the code to know mine works, he probably didn't even look at it.  So it's just the luck of the draw.  No worries.  I'm just posting to quickly vent a little bit.  Definitly not a dispute.  Heh ^^
0
 
LVL 9

Author Comment

by:tonydemarco
ID: 33623326
QCubed,

Sorry, QCubed but I did try your first code before dragon-it's accepted post and it id not work for me.

I tried your second post after awarding points to dragon-it and it did work.

I am sorry for the mix-up but dragon-it was my choice because his code worked first.

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question