We help IT Professionals succeed at work.

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
Comment
Watch Question

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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

Author

Commented:
Can this read from a file and actually spit out a new file instead of looking at a string.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Sure, I'll work that up quickly.

~bp
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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

Author

Commented:
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","")
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
That will remove a string from the line, but will still write a blank line to the output.  You might want to add the following also.

sExtract = Mid(sField, iLeft+1, iRight-iLeft-1)
sExtract=sExtract.Replace('ABC","")
If sExtract <> "" Then oOutfile.WriteLine(sExtract)

~bp

Explore More ContentExplore courses, solutions, and other research materials related to this topic.