newone2011
asked on
Parsing file with | and extracting some vals in { }
I have a file with lot of data separated by | For example:
O{bTyp | W{Hello}bUWr | W{Test data}bUid | W{1212}WCmd | W{}WId | A{}WNme |
W{How are you!}fAct | W{}fTyp | W{}fKey |
I am trying to see if I can use a vbscript that can just return me the values enclosed by { } and each value in new line. However only exception will be to ignore any O{bTyp as that does not have a matching close parenthesis.
So the script on this file would return:
Hello
Test Data
1212
How are you
O{bTyp | W{Hello}bUWr | W{Test data}bUid | W{1212}WCmd | W{}WId | A{}WNme |
W{How are you!}fAct | W{}fTyp | W{}fKey |
I am trying to see if I can use a vbscript that can just return me the values enclosed by { } and each value in new line. However only exception will be to ignore any O{bTyp as that does not have a matching close parenthesis.
So the script on this file would return:
Hello
Test Data
1212
How are you
ASKER
Can this read from a file and actually spit out a new file instead of looking at a string.
Sure, I'll work that up quickly.
~bp
~bp
Okay, this should work. It takes the input and output file names as parms, so run something like this:
cscript EE27425847.vbs "in.txt" "out.txt"
cscript EE27425847.vbs "in.txt" "out.txt"
' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
' Get input and output file names from command line parms
If (WScript.Arguments.Count > 0) Then
sInfile = WScript.Arguments(0)
Else
WScript.Echo "No input filename specified."
WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
sOutfile = WScript.Arguments(1)
Else
WScript.Echo "No output filename specified."
WScript.Quit
End If
' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")
' Open input and output files
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
' Read each input line, extract field values, write to output file
Do While Not oInfile.AtEndOfStream
sLine = oInfile.ReadLine
aFields = Split(sLine, "|")
For Each sField in aFields
iLeft = Instr(sField, "{")
iRight = Instr(sField, "}")
If iLeft > 0 And iRight > 0 And iRight-ileft > 1 Then
sExtract = Mid(sField, iLeft+1, iRight-iLeft-1)
oOutfile.WriteLine(sExtract)
End If
Next
Loop
' Cleanup and end
oInfile.Close
oOutfile.Close
Set oInfile = Nothing
Set oOutfile = Nothing
Set oFSO = Nothing
~bp
ASKER
Thanks, it looks good. I will award you the points today. One final question, if I want to eliminate certain lines from the output, based on strings, can I do this based on above code. Just after:
sExtract = Mid(sField, iLeft+1, iRight-iLeft-1)
sExtract=sExtract.Replace( 'ABC","")
sExtract = Mid(sField, iLeft+1, iRight-iLeft-1)
sExtract=sExtract.Replace(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
~bp