Link to home
Start Free TrialLog in
Avatar of newone2011
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
Avatar of Bill Prew
Bill Prew

How about something like this:

strTest = "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 |"

arrFields = Split(strTest, "|")
For Each strField in arrFields
  intLeft = Instr(strField, "{")
  intRight = Instr(strField, "}")
  If intLeft > 0 And intRight > 0 And intRight-intleft > 1 Then
    strExtract = Mid(strField, intLeft+1, intRight-intLeft-1)
    Wscript.Echo strExtract
  End If
Next

Open in new window

~bp
Avatar of newone2011

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
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"

' 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

Open in new window

~bp
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","")
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial