Solved

FINDSTR Multiple occurances of text in order

Posted on 2010-09-07
15
505 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now