473,387 Members | 1,504 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,387 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 2071
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.