Login | |
|
 |
Re: Reading contents of text file and search ... - 6/30/2005 8:56:28 AM
|
|
 |
|
| |
Xandros
Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
|
Because you have multple "styles" of data that you're trying to locate/extract you need more a more flexible set of parsing rules. The following script demonstrates how to parse the 3 styles indicated by your sample screen data... Option Explicit Dim InputFile Dim FSO, oFile, LineIn, TempLine Dim MyRegion, MySpeed, MyDomain Dim MyIPa, MyIPb, MyRefNo InputFile = "C:\My Documents\Scripts\Copy.txt" MyRegion = "" : MySpeed = "" : MyDomain = "" MyIPa = "" : MyIPb = "" : MyRefNo = "" Set FSO = CreateObject("Scripting.FileSystemObject") Set oFile = FSO.OpenTextFile(InputFile) Do Until oFile.AtEndOfStream LineIn = oFile.ReadLine TempLine = LCase(LineIn) If FindString("region", MyRegion) Then ElseIf FindSpeed("speed", MySpeed) Then ElseIf FindString("domain", MyDomain) Then ElseIf FindString("ip address a", MyIPa) Then ElseIf FindString("ip address b", MyIPb) Then ElseIf FindString2("reference number", MyRefNo) Then End If Loop wscript.echo "MyRegion = " & MyRegion wscript.echo "MySpeed = " & MySpeed wscript.echo "MyDomain = " & MyDomain wscript.echo "MyIPa = " & MyIPa wscript.echo "MyIPb = " & MyIPb wscript.echo "MyRefNo = " & MyRefNo oFile.Close Set oFile = Nothing Set FSO = Nothing Function FindString(sSearch, ByRef MyReturn) Dim Ipos FindString = False iPos = InStr(TempLine, sSearch) If iPos > 0 Then MyReturn = Trim(Mid(LineIn, iPos + Len(sSearch) + 1)) FindString = True End If End Function Function FindString2(sSearch, ByRef MyReturn) Dim Ipos FindString2 = False iPos = InStr(TempLine, sSearch) If iPos > 0 Then On Error Resume Next ' protect against read past eof MyReturn = oFile.ReadLine If Err.Number = 0 Then FindString2 = True End If On Error Goto 0 End If End Function Function FindSpeed(sSearch, ByRef MyReturn) Dim Ipos, SpeedCode FindSpeed = False iPos = InStr(TempLine, "speed") If iPos > 0 Then FindSpeed = True SpeedCode = Trim(Mid(TempLine, iPos + 5)) If InStr(SpeedCode, "1.5mbps") Then MyReturn = "150" ElseIf InStr(SpeedCode, "512kbps") Then MyReturn = "512" ElseIf InStr(SpeedCode, "128kbps") Then ' or whatever else... MyReturn = "128" Else MyReturn = "OOPS!" End If End If End Function
|
|
| |
|
|
|
 |
Re: Reading contents of text file and search ... - 6/30/2005 11:57:52 AM
|
|
 |
|
| |
BBistheKing
Posts: 107
Score: 0
Joined: 6/21/2005
Status: offline
|
You are a legend, Xandros. Thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you.
|
|
| |
|
|
|
 |
Re: Reading contents of text file and search ... - 7/1/2005 8:40:34 AM
|
|
 |
|
| |
didorno
Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
|
BBistheKing, I played a little bit with your question and find a solution (I think) with regular expressions. This gives a lot more flexibility in adaptation of the searches, as Xandros already stressed in his post from 06/30/2005 : 2:56:28 PM. This is the original text file (TestTxt.txt) I use as example : Part of the screen data will look something like this (there will be lots of other text before and after that I don't need): Region Regional1 Speed 512Kbps/128Kbps Domain N7774444K IP Address A 222.333.444.555 IP Address B 444.555.666.777 I need the data to the right. Some data appears like this: Reference Number WR222555 <-----The number is what I would like to get Great spirits have always encountered violent opposition from mediocre minds This is my script : conLog = "X:\Your path to\TestText.txt" conForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.GetFile(conLog) If objFile.Size > 0 Then Set ts = objFSO.OpenTextFile(conLog, conForReading) ReadFile= ts.ReadAll ts.Close End If Var1 = RegExpTest("Region ", ReadFile) Var2 = RegExpTest("speed ", ReadFile) Var3 = RegExpTest("domain ", ReadFile) Var4 = RegExpTest("ip address a ", ReadFile) Var5 = RegExpTest("ip address b ", ReadFile) Var6 = RegExpTest("reference number" & "\r\n" & "WR", ReadFile) WScript.Echo Var1 & vbCrLf & Var2 & vbCrLf & Var3 & vbCrLf & Var4 & vbCrLf & Var5 & vbCrLf & Var6 Function RegExpTest(patrn, strng) Dim regEx, Match, Matches ' Create variable. Set regEx = New RegExp ' Create a regular expression. regEx.Pattern = patrn ' Set pattern. regEx.IgnoreCase = True ' Set case insensitivity. regEx.Global = True ' Set global applicability. Set Matches = regEx.Execute(strng) ' Execute search. For Each Match in Matches ' Iterate Matches collection. BeginPos = Match.FirstIndex + Match.Length + 1 ' FirstIndex has zero-based offset EndPosCrLf = InStr(BeginPos, strng, vbCrLf) ' Found item ends with CrLf EndPosSpace = InStr(BeginPos, strng, Space(1)) ' Found item ends with Space If EndPosCrLf > EndPosSpace Then ' First occurrence of CrLf or Space EndPos = EndPosSpace Else EndPos = EndPosCrLf End If RetStr = Mid(strng, BeginPos, EndPos-BeginPos) Next RegExpTest = RetStr End Function This is the (last part of the) final output Regional1 512Kbps/128Kbps N7774444K 222.333.444.555 444.555.666.777 222555 I have left the change from real to hundred fold integer out (for the time being) Regards.
< Message edited by didorno -- 7/20/2005 5:53:20 AM >
|
|
| |
|
|
|
 |
Re: Reading contents of text file and search ... - 7/1/2005 10:25:01 PM
|
|
 |
|
| |
didorno
Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
|
ehvbs, thank you ! You show an interesting approach, but I do not understand the "?" parameter. Can you explain this ? It is possible to exploit regular expressions still better and be more flexible in the next way. conLog = "X:\Your path to\TestText.txt" conForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.GetFile(conLog) If objFile.Size > 0 Then Set ts = objFSO.OpenTextFile(conLog, conForReading) ReadFile= ts.ReadAll ts.Close End If Var1 = RegExpTest("Region (\S+)", ReadFile) ' "Region " followed by submatch of 1 or more non-whitespace characters Var2 = RegExpTest("Speed (\S+)", ReadFile) ' "Speed " ,, ,, ,, ,, Var3 = RegExpTest("Domain (\S+)", ReadFile) ' "Domain " ,, ,, ,, ,, Var4 = RegExpTest("IP Address A (\S+)", ReadFile) ' "IP Address A " ,, Var5 = RegExpTest("IP Address B (\S+)", ReadFile) ' "IP Address B " ,, Var6 = RegExpTest("Reference Number" & "\r\n" & "WR(\S+)", ReadFile) ' "Reference Number" followed by ' Carriage Return & NewLine followed by "WR" followed by 1 or more non-whitespace characters. WScript.Echo Var1 & vbCrLf & Var2 & vbCrLf & Var3 & vbCrLf & Var4 & vbCrLf & Var5 & vbCrLf & Var6 Function RegExpTest(patrn, strng) Dim regEx, Match, Matches ' Create variable. Set regEx = New RegExp ' Create a regular expression. regEx.Pattern = patrn ' Set pattern. regEx.IgnoreCase = True ' Set case insensitivity. regEx.Global = True ' Set global applicability. Set Matches = regEx.Execute(strng) ' Execute search. For Each Match in Matches ' Iterate Matches collection. RegExpTest = Match.SubMatches(0) ' Delivers the first submatch of the matches Next End Function ' RegExpTest In principle this is all you need and easy to adapt to specific wishes. Good luck.
< Message edited by didorno -- 7/20/2005 6:03:19 AM >
|
|
| |
|
|
|
 |
Re: Reading contents of text file and search ... - 7/3/2005 1:45:34 AM
|
|
 |
|
| |
didorno
Posts: 361
Score: 0
Joined: 2/12/2005
From:
Status: offline
|
Thank you very much, ehvbs, for the links, I can use that certainly ! BBistheKing, quote: didorno, I also wanted to ask something about your code to find multiple variables of "Region" etc. What part of the code actually performs that task? I'm not sure which part, but is it the extra parentheses in the RegExpTest?
In fact, the script from 07/02/2005 : 04:25:01 AM does find all matches, but due to the final use of WScript.Echo with the Var's, only the last value of each Var is shown. By shifting the WScript.Echo within the For-loop of the function you get all the values (script 07/02/2005 : 08:09:24 AM). The additional parentheses gives the opportunity to use that parts later, for instance for showing the names of the variables (indicated by Match.SubMatches(0)). I think a minor disadvantage of the latter code is the not-logical order of the results (1st all the regions, next all the speeds, etc. in the text file). I composed the next code to solve this, the matches shown are in the order of occurrences in the text file. conLog = "X:\Your path to\TestText.txt" conForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.GetFile(conLog) Set ts = objFSO.OpenTextFile(conLog, conForReading) ReadFile = ts.ReadAll ts.Close Var = RegExpTest("(Region )(\S+)|(Speed )(\S+)|(Domain )(\S+)|(IP Address A )(\S+)" _ & "|(IP Address B )(\S+)|(Reference Number" & "\r\n" & "WR)(\S+)") Function RegExpTest(patrn) Dim regEx, Matches, Match, i, SubStr Set regEx = New RegExp regEx.Pattern = patrn regEx.IgnoreCase = True regEx.Global = True Set Matches = regEx.Execute(ReadFile) For Each Match in Matches i = 0 Do While i < 12 SubStr = Match.SubMatches(i) If SubStr <> "" Then Wscript.Echo SubStr & vbTab & vbTab & ": " & Match.SubMatches(i+1) i = i + 2 Loop Next End Function ' RegExpTest Regards.
< Message edited by didorno -- 7/20/2005 6:05:58 AM >
|
|
| |
|
|
|
|
|