Avatar of tonydemarco
tonydemarco
Flag for United States of America asked on

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

Microsoft DOSProgrammingWindows Batch

Avatar of undefined
Last Comment
tonydemarco

8/22/2022 - Mon
Steve Knight

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
Steve Knight

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
tonydemarco

ASKER

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Steve Knight

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.
tonydemarco

ASKER
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
Ben Personick (Previously QCubed)

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

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Steve Knight

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Steve Knight

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
Ben Personick (Previously QCubed)

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

tonydemarco

ASKER
Spot On!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
tonydemarco

ASKER
QCubed,

Thanks for your response.
Ben Personick (Previously QCubed)

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? ^^
Steve Knight

Qcubed, sorry I didn't even see your until after, we clearly were writing at the same time.

Steve
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ben Personick (Previously QCubed)

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 ^^
tonydemarco

ASKER
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.