FINDSTR Multiple occurances of text in order

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

LVL 9
tonydemarcoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
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
Steve KnightIT ConsultancyCommented:
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
tonydemarcoAuthor Commented:

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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Steve KnightIT ConsultancyCommented:
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
tonydemarcoAuthor Commented:
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
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
Steve KnightIT ConsultancyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve KnightIT ConsultancyCommented:
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
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
tonydemarcoAuthor Commented:
Spot On!
0
tonydemarcoAuthor Commented:
QCubed,

Thanks for your response.
0
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
Steve KnightIT ConsultancyCommented:
Qcubed, sorry I didn't even see your until after, we clearly were writing at the same time.

Steve
0
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
tonydemarcoAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.