Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex and reading text file 1

Status
Not open for further replies.

stevio

Vendor
Jul 24, 2002
78
AU
Have a text file which looks like the following:

txt1 1001
txt1 1002
txt1 1003
txt1 1004

txt2 1005
txt2 1006
txt2 1007
txt2 1008
txt2 1009

txt3 1010
txt3 1011
txt3 1012

etc
Each block can have different number of lines

I want to be able to loop through each line of the file. On the first pass, I want to grab the first line from the first block, first line from the second block etc and use the number eg 1001, 1005, 1010 etc.

On the second pass, grab the second line from the first block, sesond line from the second block etc and use the number

I'm think you probably need to tag the ones that have been done

*txt1 1001
.
.
*txt2 1005

The goal is the grab a number(which is unique) as long as it is from a separate block until eof. Any ideas?

The text file does not have to have the formatting above - can have gaps or no gaps between blocks

Your help would be greatly appreciated
 
A sufficient flexible way is to through those "block" into dictionary object with the characteristic string "txtn" as key. Like this.
[tt]
dim datafile, s
datafile="d:\test\xyz.txt" 'the data file path
s=createobject("scripting.filesystemobject").opentextfile(datafile, 1, true, -2).readall

dim rx
set rx=new regexp
rx.global=true

dim dcm
set dcm=createobject("scripting.dictionary")

'since the max n appears in txtn is unknown, you may want to scan a range, let's say n=1 to 10
dim scan_min, scan_max, i, cm
scan_min=1
scan_max=10
for i=scan_min to scan_max
rx.pattern="\btxt" & i & "\b\s+\b(\d*)\b"
set cm=rx.execute(s)
if cm.count<>0 then
dcm.add "txt" & i, cm
end if
next

'to illustrate of retrieval of info for txt2 say
dim skey
skey="txt2"
if dcm.exists(skey) then
set cm=dcm(skey)
for i=0 to dcm(skey).count-1
wscript.echo cm(i)
next
end if
[/tt]
But, I fear you might not be able to make good use unless you know vbs more fluently.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top