Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

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

0
tonydemarco
Asked:
tonydemarco
  • 6
  • 5
  • 4
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now