473,381 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 software developers and data experts.

Searching a txt file for instances of a string

I have a text file that contains the following:
********************

__StartCustomerID_41

Name: Fred Smith
Address: 57 Pew Road
Croydon
Surrey
PostCode: CR0 3RT

__EndCustomerID_41
__StartCustomerID_86

Name: Mary Jones
Address: Flat 6
14 Honor Avenue
Biggin Hill
Westerham
Kent
PostCode: TN16 4PQ

__EndCustomerID_86
__StartCustomerID_234

Name: Sally Williams
Address: 1 Gap Lane
Bromley
Kent
PostCode: BR5 4TD

__EndCustomerID_234
********************

I can loop through the text file and grab the Name & Postcode fields by
using:

If InStr(1, strCurrentLineOfTextFile, ":") then blah blah

But how to I grab the Address field?

I guess I want some code that says "At the end of the line containing the
string "Name: " grab the text until the start of the line containing the
string "PostCode: "

But how do I do this? Help!

Paul
Nov 13 '05 #1
3 2070
Paul H wrote:
I have a text file that contains the following:
********************

__StartCustomerID_41

Name: Fred Smith
Address: 57 Pew Road
Croydon
Surrey
PostCode: CR0 3RT

__EndCustomerID_41
__StartCustomerID_86

Name: Mary Jones
Address: Flat 6
14 Honor Avenue
Biggin Hill
Westerham
Kent
PostCode: TN16 4PQ

__EndCustomerID_86
__StartCustomerID_234

Name: Sally Williams
Address: 1 Gap Lane
Bromley
Kent
PostCode: BR5 4TD

__EndCustomerID_234
********************

I can loop through the text file and grab the Name & Postcode fields by
using:

If InStr(1, strCurrentLineOfTextFile, ":") then blah blah

But how to I grab the Address field?

I guess I want some code that says "At the end of the line containing the
string "Name: " grab the text until the start of the line containing the
string "PostCode: "

But how do I do this? Help!

Paul


I suppose you could do an Instr() on the word Address and an Instr() on
PostCode. then parse out the lines searching for NewLines.

If "Address: " was 20, then you'd want to add 8 to thatto get the start
of the address.

If "PostCode:" was 50, then you'd want to subract 49 from 28 to get the
length of the address.

Then you'd use Mid() to get the address string. Then you'd need to get
the strings delimited by newline characters.

But I don't understand why you don't use Open/Close,Input to get this
informattion. Read it line by line. Name: starts a new record,
PostCode ends a record. When you hit Address:, read each line till you
hit PostCode to get your address info,

Reading and parsing a text file like yours and doing it with Instr()
seems to be a PITA.
Nov 13 '05 #2
On Fri, 27 May 2005 18:41:48 GMT, Salad <oi*@vinegar.com> wrote:
Paul H wrote:
I have a text file that contains the following:
********************

__StartCustomerID_41

Name: Fred Smith
Address: 57 Pew Road
Croydon
Surrey
PostCode: CR0 3RT

__EndCustomerID_41
__StartCustomerID_86

Name: Mary Jones
Address: Flat 6
14 Honor Avenue
Biggin Hill
Westerham
Kent
PostCode: TN16 4PQ

__EndCustomerID_86
__StartCustomerID_234

Name: Sally Williams
Address: 1 Gap Lane
Bromley
Kent
PostCode: BR5 4TD

__EndCustomerID_234
********************

I can loop through the text file and grab the Name & Postcode fields by
using:

If InStr(1, strCurrentLineOfTextFile, ":") then blah blah

But how to I grab the Address field?

I guess I want some code that says "At the end of the line containing the
string "Name: " grab the text until the start of the line containing the
string "PostCode: "

But how do I do this? Help!

Paul


I suppose you could do an Instr() on the word Address and an Instr() on
PostCode. then parse out the lines searching for NewLines.

If "Address: " was 20, then you'd want to add 8 to thatto get the start
of the address.

If "PostCode:" was 50, then you'd want to subract 49 from 28 to get the
length of the address.

Then you'd use Mid() to get the address string. Then you'd need to get
the strings delimited by newline characters.

But I don't understand why you don't use Open/Close,Input to get this
informattion. Read it line by line. Name: starts a new record,
PostCode ends a record. When you hit Address:, read each line till you
hit PostCode to get your address info,

Reading and parsing a text file like yours and doing it with Instr()
seems to be a PITA.


I think the OP already has a loop set up to read a new record. The
variable "strCurrentLineOfTextFile" strongly implies that this is so.

Hence, what he needs to do is to implement exactly what you have
described. That would typically be done by a secondary test/loop.
The exact language depends on the "blah blah".

It appears that the current code is intended to allow the blank lines
to be ignored. If that is so, the secondary loop would ensure that
the lines after the Address: line and before the Postcode: line are
used, rather than ignored.

Let's assume that the OP is using a Select statement as the basis of
the blah blah (what follows is aircode ... I am horrible at
remembering off the top of my head what the ending instructions are
for loops and such, so confirm that what I have used is correct before
blaming me)

The result he is after might look something like this:
'set up a boolean variable named boolProcessingAddress
' and set it to false
'set up a boolean variable named boolNOT_AT_EOF
' and set it to true
....
do while boolNOT_AT_EOF
Select Case InStr(1, strCurrentLineOfTextFile, ":")
Case 0
' This line does not have a colon in it (anywhere)
' See whether you are currently processing the group between
the address and the postcode
if boolProcessingAddress then
' this is a new line for the address, go ahead with it
endif
Case else
' Do whatever you currently do with your input lines
' For example, you might test to see whether the line starts
' with PostCode:
' If you find a line that starts with Address:, then
' in addition to the processing you are doing now, you
' will want to:
Set boolProcessingAddress = True
' Otherwise, if you find a line that doesn't start with
Address:, then
' in addition to the processing you are doing now, you
' will want to:
Set boolProcessingAddress = False
End Select
' do whatever you do to get a new strCurrentLineOfTextFile
' set boolNOT_AT_EOF to false if at end of file
...
loop

Testing for a colon ANYWHERE in the line is a recipe for disaster. If
your other lines have an address with a : buried in there, your entire
routine will crash.

mike
Nov 13 '05 #3

"Mike Preston" <mb******@pacbell.invalid.net> wrote in message
news:42****************@news.INDIVIDUAL.NET...
On Fri, 27 May 2005 18:41:48 GMT, Salad <oi*@vinegar.com> wrote:
Paul H wrote:
I have a text file that contains the following:
********************

__StartCustomerID_41

Name: Fred Smith
Address: 57 Pew Road
Croydon
Surrey
PostCode: CR0 3RT

__EndCustomerID_41
__StartCustomerID_86

Name: Mary Jones
Address: Flat 6
14 Honor Avenue
Biggin Hill
Westerham
Kent
PostCode: TN16 4PQ

__EndCustomerID_86
__StartCustomerID_234

Name: Sally Williams
Address: 1 Gap Lane
Bromley
Kent
PostCode: BR5 4TD

__EndCustomerID_234
********************

I can loop through the text file and grab the Name & Postcode fields by
using:

If InStr(1, strCurrentLineOfTextFile, ":") then blah blah

But how to I grab the Address field?

I guess I want some code that says "At the end of the line containing
the
string "Name: " grab the text until the start of the line containing the
string "PostCode: "

But how do I do this? Help!

Paul


I suppose you could do an Instr() on the word Address and an Instr() on
PostCode. then parse out the lines searching for NewLines.

If "Address: " was 20, then you'd want to add 8 to thatto get the start
of the address.

If "PostCode:" was 50, then you'd want to subract 49 from 28 to get the
length of the address.

Then you'd use Mid() to get the address string. Then you'd need to get
the strings delimited by newline characters.

But I don't understand why you don't use Open/Close,Input to get this
informattion. Read it line by line. Name: starts a new record,
PostCode ends a record. When you hit Address:, read each line till you
hit PostCode to get your address info,

Reading and parsing a text file like yours and doing it with Instr()
seems to be a PITA.


I think the OP already has a loop set up to read a new record. The
variable "strCurrentLineOfTextFile" strongly implies that this is so.

Hence, what he needs to do is to implement exactly what you have
described. That would typically be done by a secondary test/loop.
The exact language depends on the "blah blah".

It appears that the current code is intended to allow the blank lines
to be ignored. If that is so, the secondary loop would ensure that
the lines after the Address: line and before the Postcode: line are
used, rather than ignored.

Let's assume that the OP is using a Select statement as the basis of
the blah blah (what follows is aircode ... I am horrible at
remembering off the top of my head what the ending instructions are
for loops and such, so confirm that what I have used is correct before
blaming me)

The result he is after might look something like this:
'set up a boolean variable named boolProcessingAddress
' and set it to false
'set up a boolean variable named boolNOT_AT_EOF
' and set it to true
...
do while boolNOT_AT_EOF
Select Case InStr(1, strCurrentLineOfTextFile, ":")
Case 0
' This line does not have a colon in it (anywhere)
' See whether you are currently processing the group between
the address and the postcode
if boolProcessingAddress then
' this is a new line for the address, go ahead with it
endif
Case else
' Do whatever you currently do with your input lines
' For example, you might test to see whether the line starts
' with PostCode:
' If you find a line that starts with Address:, then
' in addition to the processing you are doing now, you
' will want to:
Set boolProcessingAddress = True
' Otherwise, if you find a line that doesn't start with
Address:, then
' in addition to the processing you are doing now, you
' will want to:
Set boolProcessingAddress = False
End Select
' do whatever you do to get a new strCurrentLineOfTextFile
' set boolNOT_AT_EOF to false if at end of file
...
loop

Testing for a colon ANYWHERE in the line is a recipe for disaster. If
your other lines have an address with a : buried in there, your entire
routine will crash.

mike


I knew I needed a second loop, what I hadn't thought of was the Boolean
variable.I just had a complete mental block. Also, thanks for the tip on NOT
searching for *just* a colon, I have corrected that too.

Many thanks Mike you've been a great help

Paul
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael | last post by:
Hi all, I have a html file that i'm trying to get data from, i need to search for a specific string/tag and read the text between it and the close tag. the setout of the data is like <td>...
14
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
60
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't...
2
by: Trint Smith | last post by:
How can I store an entire html file in a string with all it's formating, double quotes, multiple lines and all?? like this: Dim FileString As String = ENTIRE HTML FILE thanks, Trint .Net...
1
by: warheart | last post by:
hi im kinda new to programming i aint lazy to search and learn :D but i am getting despirate... ive been looking everywhere for a code to search a txt file and find a string, and then give...
3
by: Paul73 | last post by:
This is driving me nuts. Someone please help me with this. I have a program that's reading a list of terms to search for. The .IndexOf works fine if the term being searched for is in the first...
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () A...
0
by: Gabriel Genellina | last post by:
En Mon, 18 Aug 2008 17:40:13 -0300, Alexnb <alexnbryan@gmail.comescribió: pydata = """() A registry mark given by underwriters (as at Lloyd's) to ships in .... first-class condition. Inferior...
3
by: jain236 | last post by:
Hi , i have file of 32kb , i want to read the whole file into string , i tried this by doing the below code, but i dint got the whole content of the file in the string , i guess the variable is not...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.