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!

Question about VBS and RegExp

Status
Not open for further replies.

areotree

Technical User
Feb 16, 2003
24
US
Hello everybody. I am new to VBS and to using RegExp. I have a collection script that grabs data from a Passport switch into multipliable files. I need to parse the data in the files. I put together a function to do this based on what I have found in this forum and the following link: Below is the function and examples of the files and the end result after the files are parsed. The function apears to work fine but I am sure there is something I am missing in the RegExp. Could you look at what I have and tell me if there is a better way. Also thanks for all the info on the this forum

Files before parsing:
File1:

617>

617> d -o laps/*

Laps/*
Use -noTabular to see hidden attributes: osiUnknw, osiStby, osiAlarm,
osiCntrl, osiProc, osiAvail, ps, psmAlarm, mmAlarm, pfAlarm and
timeUntilRestore.
+=====+-----+----+-----+------+------+------+------+------+-------+-----------
|Laps |osiAd|osiO|osiUs|neRxLi|neReq |neReqC|feReq |feReqC|sdOnLin|switchovers
| | min |per | age | ne | | han | | han | es |
+=====+-----+----+-----+------+------+------+------+------+-------+-----------
| 1000|unlck|ena |busy |workin|noRequ|protec|noRequ|protec| | 2
| 1001|unlck|ena |busy |workin|noRequ|protec|noRequ|protec| | 0
| 1002|unlck|ena |busy |workin|noRequ|protec|noRequ|protec| | 0
| 1003|unlck|ena |busy |workin|noRequ|protec|noRequ|protec| | 0
| 1200|unlck|ena |busy |workin|noRequ|protec|noRequ|protec| | 6
| 1300|unlck|ena |busy |workin|noRequ|protec|doNotR|protec| | 0
ok 2007-11-22 22:45:58.21

File2:
618>

618> d -o laps/* sts/*

Laps/* Sts/*
Use -noTabular to see the many hidden attributes.
+=====+===+-----+----+-----+------+---+---+---+---+---+---+----------
|Laps |Sts|osiAd|osiO|osiUs|snmpOp|lop|ais|rfi|slm|txA|txR| pefs
| | | min |per | age |erStat| | | | |is |di |
| | | | | | us | | | | | | |
+=====+===+-----+----+-----+------+---+---+---+---+---+---+----------
| 1000| 0|unlck|ena |busy |up |off|off|off|off|off|off| 1656189
| 1001| 0|unlck|ena |busy |up |off|off|off|off|off|off| 1656191
| 1002| 0|unlck|ena |busy |up |off|off|off|off|off|off| 1656189
| 1003| 0|unlck|ena |busy |up |off|off|off|off|off|off| 1656189
| 1200| 0|unlck|ena |busy |up |off|off|off|off|off|off| 886195
| 1300| 0|unlck|ena |busy |up |off|off|off|off|off|off| 568069
ok 2007-11-22 22:45:59.84

Files after parsing:
File1:
1000;unlck;ena;busy;workin;noRequ;protec;noRequ;protec;;2
1001;unlck;ena;busy;workin;noRequ;protec;noRequ;protec;;0
1002;unlck;ena;busy;workin;noRequ;protec;noRequ;protec;;0
1003;unlck;ena;busy;workin;noRequ;protec;noRequ;protec;;0
1200;unlck;ena;busy;workin;noRequ;protec;noRequ;protec;;6
1300;unlck;ena;busy;workin;noRequ;protec;doNotR;protec;;0
ok2007-11-2222:45:58.21

File2:
1000;0;unlck;ena;busy;up;off;off;off;off;off;off;1656189
1001;0;unlck;ena;busy;up;off;off;off;off;off;off;1656191
1002;0;unlck;ena;busy;up;off;off;off;off;off;off;1656189
1003;0;unlck;ena;busy;up;off;off;off;off;off;off;1656189
1200;0;unlck;ena;busy;up;off;off;off;off;off;off;886195
1300;0;unlck;ena;busy;up;off;off;off;off;off;off;568069
ok2007-11-2222:45:59.84

Here is the function, I am loading the file then reading each line.

===============================================

Function FileParser (filenme)
' On Error Resume Next

Dim regExp1 : Set regExp1 = New RegExp
regExp1.Pattern = "(|)?\d+.(|)?"
regExp1.IgnoreCase = True

Set filEtextFile = filesys.GetFile(filenme)
Set testFile = filEtextFile.OpenAsTextStream(ForReading)
Report="" 'the idea to be proper requires reset to empty'

Do Until testFile.AtEndOfStream
varLine = testFile.ReadLine
If (regExp1.Test(varLine)) And (Len(Trim(varLine)) > 5) And (InStr(varLine, ">") = False) Then
If (Left(varLine,1) = "|") Then
Report = Report & Mid(Trim(varLine),2) & vbCrLf
Else
Report = Report & Trim(varLine) & vbCrLf'
End If
End If
Loop
Do While Instr(Report, " ")
Report = Replace(Report, " ", "")
Loop
'Change delimiter
Report = Replace(Report, "|", ";")

''''''''''Write the changed text back to the file
Set testFile = filEtextFile.OpenAsTextStream(ForWriting)
testFile.Write Report
testFile.Close
End Function
 
1) You probably need an ^ to indicate start of line so the pattern would be "^(|)?\d+.(|)?".

2) You shouldn't need

And (Len(Trim(varLine)) > 5) And (InStr(varLine, ">") = False)"

in the if statement since the regexp should find the line.

3) You could split up the line using the split function, using "|" as a delimiter.
 
I tried the "^" at the first of the pattern but for some reason it failed to match any line until I removed it. The "(Len(Trim(varLine)) > 5)" is because I don't want lines with less then 5 char. Is there a way to put that in the RegExp pattern so I don't have to do the "(Len(Trim(varLine)) > 5)" ?

Oh, one more thing. I have found is that some of the files have numbers in the header part of the data which I do not want. Any ideas on how to parse this out. Below is an example of what I am talking about. I don't want to have to put specific word searches in if I can help it. What I would like to do is figure out a pattern that would get just the data I need.

All help is appreciated. I have some ProComm Aspect scripts for Nortel nodes if anyone needs them. :)

Before parsing:

20>

20> d -o nsta/* vgs h248/*

Nsta/* Vgs H248/*
Use -noTabular to see the many hidden attributes.
+=====+====+----+-----+-----+-----+-----------+-----------+------
|Nsta |H248|vers|ctxts|time |cTime|mgProvision|mgcProvisio| mit
| | | | | msec| msec|alRespTimer|nalRespTime| msec
| | | | | | | Value | rValue |
| | | | | | | msec| msec|
+=====+====+----+-----+-----+-----+-----------+-----------+------
| 12| 0| 1| 2100| 1000| 500| 10000| 500| 60000
| 13| 0| 1| 2100| 1000| 500| 10000| 500| 60000
| 15| 0| 1| 300| 1000| 500| 10000| 500| 60000
ok 2007-11-23 18:52:49.85

After:

Nsta;H248;vers;ctxts;time;cTime;mgProvision;mgcProvisio;mit
12;0;1;2100;1000;500;10000;500;60000
13;0;1;2100;1000;500;10000;500;60000
15;0;1;300;1000;500;10000;500;60000
ok2007-11-2318:52:49.85
 
Also I changed the code a little bit in the function. It now loads all the files in a dedicated dir. for the capture files. It is posted below is case someone wants it. I put it together from multipliable post on this forum.

Thanks Everybody.

====================================

Function FileParser
' On Error Resume Next

Dim regExp1 : Set regExp1 = New RegExp
regExp1.Pattern = "(|)?\d+.(|)?"
regExp1.IgnoreCase = True
Set objFolder = filesys.GetFolder("C:\CRT_logs\R4_Logs\")
For Each objfile in objFolder.Files
Set testFile = filesys.OpenTextFile(objFile.path, ForReading)
Wshshell.Popup objfile,1, "Processing file:"

Report="" 'the idea to be proper requires reset to empty'

Do Until testFile.AtEndOfStream
varLine = testFile.ReadLine
If (regExp1.Test(varLine)) And (Len(Trim(varLine)) > 5) And (InStr(varLine, "/*") = False) And (InStr(varLine, ">") = False) Then
If (Left(varLine,1) = "|") Then
Report = Report & Mid(Trim(varLine),2) & vbCrLf
Else
Report = Report & Trim(varLine) & vbCrLf'
End If
End If
Loop
Do While Instr(Report, " ")
Report = Replace(Report, " ", "")
Loop
'Change delimiter
Report = Replace(Report, "|", ";")

''''''''''Write the changed text back to the file
Set testFile = filesys.OpenTextFile(objFile.path, ForWriting)
testFile.Write Report
testFile.Close
Next

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top