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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parsing problem has got me stumped 2

Status
Not open for further replies.

ktucci

Programmer
Apr 23, 2001
146
US
i have this text file and i am trying to parse from each occurence of ALN to the next occurence into 4 fields...i am reading through the file LINE BY LINE and i am having problems figuring this one out.

this is hwo i am opeing the file...i would like to use this same method and continue to read the file line by line

Open str_path & str_filename For Input As #filehandle
Do While EOF(filehandle) = False
Line Input #filehandle, dataline

the desired parsed output would look something like:

2123971354,1,ALN,/TN 212 397-1354/LPIC NYC /LPCX 0698/LPCA SN, 1-27-99 /PIC ATX/PICX 0288,.89

2123971354,1,NBJ,/TN 212 397-1354/PRIV Y,1.22

2123971354,1,POR2X,/TN 212 397-1354,3.23

2123971354,1,RTVX6,/TN 212 397-1354/LCC SBM,1.11

2123971405,1,ALN,/TN 212 397-1405/LPIC NYC /LPCX 0698/LPCA SN, 1-27-99 /PIC ATX/PICX 0288 /PCA SN, 1-27-99,.67

2123971405,1,NBJ,/TN 212 397-1405/PRIV Y,0

and so on...the trick part for me is where the ALN items since the detail portion of that data may run on to multiple lines with amount after the last line...below is a small portion of the source text file...any help in pointing me in the right direction will be greatly appreciated...thanks keith


-----------------------------------------------------

1 ALN /TN 212 397-1354/LPIC NYC
/LPCX 0698/LPCA SN, 1-27-99
/PIC ATX/PICX 0288
/PCA SN, 1-27-99 T .89
1 NBJ /TN 212 397-1354/PRIV Y T 1.22
1 POR2X /TN 212 397-1354 T 3.23
1 RTVX6 /TN 212 397-1354/LCC SBM T 1.11
1 ALN /TN 212 397-1405/LPIC NYC
/LPCX 0698/LPCA SN, 1-27-99
/PIC ATX/PICX 0288
/PCA SN, 1-27-99 T .67
1 NBJ /TN 212 397-1405/PRIV Y T 0
1 POR2X /TN 212 397-1405 T 0
1 RTVX6 /TN 212 397-1405/LCC SBM T 0
1 ALN /TN 212 397-1431/LPIC NYC
/LPCX 0698/LPCA SN, 1-27-99
/PIC ATX/PICX 0288
/PCA SN, 1-27-99 T 0
1 NBJ /TN 212 397-1431/PRIV Y T 0
1 POR2X /TN 212 397-1431 T 0
1 RTVX6 /TN 212 397-1431/LCC SBM T 0
1 TCG /TN 212 397-1492/HTG 1 /GST
/LPIC NYC/LPCX 0698
/LPCA SN, 1-27-99/PIC ATX
/PICX 0288/PCA SN, 1-27-99 T 0
1 NBJ /TN 212 397-1492/PRIV Y T 0
1 PORPX /TN 212 397-1492 T 0
1 RTVX6 /TN 212 397-1492/LCC SBM T 0
1 TJB /TN 212 397-1492 T 0
 
Just out of curiousity, what generates the text file and is the "ALN" always going to be a set number of characters from the start of the line? Anything is possible, the problem is I only have one lifetime.
[cheers]
 
unfortunately i do not know what generates the file(it is from a telco carrier), but the ALN is a fixed number of characters from the start of the line
 
This might take care of what you need

If Mid$(sLineData,iFixedStartPoint,3) = "ALN" Then

'Read next 4 lines

End If

where sLineData is the line read from the text file and iFixedStartPoint is the number of charaters before the "ALN" shows up in the text file.
I hope I am understanding your question correctly. Good Luck. Anything is possible, the problem is I only have one lifetime.
[cheers]
 
let me show you what i have so far...the problem i have is not really locating the ALN but the detail section after the ALN and concatonating the data from the detail from each line and then knowing when i am done with the ALN and I am on a new line

Public Sub parse_file(str_filename As String)
On Error GoTo getout
Dim filehandle As Integer
Dim dataline As String
Dim str_btn As String
Dim str_account_section As String
Dim str_btn_done As String
Dim str_account_section_done As String
Dim str_aln_count As Integer
Dim str_alm_count As Integer
Dim str_als_count As Integer

Dim str_as_1 As String
Dim str_as_2 As String
Dim str_tmp_as_1 As String
Dim str_tmp_as_2 As String

Dim str_as_save As String



str_btn = ""
str_account_section = ""
str_btn_done = 0
str_account_section_done = 0

'str_as_1 = ""
'str_as_2 = ""
'str_as_3 = ""



filehandle = FreeFile
Open str_path & str_filename For Input As #filehandle
Do While EOF(filehandle) = False
Line Input #filehandle, dataline

'''''Get BTN'''''''''''''''

If str_btn_done = 0 Then
If InStr(dataline, &quot; Remarks &quot;) <> 0 Then
str_btn = Mid(dataline, InStr(dataline, &quot; Remarks &quot;) + 10, 12)
str_btn = Replace(Replace(str_btn, &quot; &quot;, &quot;&quot;), &quot;-&quot;, &quot;&quot;)
str_btn_done = 1
End If
End If

'MsgBox &quot;-&quot; & str_btn & &quot;-&quot;

''''''Account Section''''''''

If str_btn_done = 1 Then
If str_account_section_done = 0 Then
If InStr(dataline, &quot;---S&E&quot;) <> 0 Then
str_as_save = str_btn & &quot;,&quot; & str_as_1 & &quot;,&quot; & str_as_2
MsgBox &quot;end of acct sect&quot;
str_account_section_done = 1
Else
If Mid(dataline, 30, 4) = &quot;RSID&quot; Or Mid(dataline, 30, 2) = &quot;LN&quot; Or Mid(dataline, 30, 2) = &quot;LA&quot; Or Mid(dataline, 30, 3) = &quot;LOC&quot; Or Mid(dataline, 30, 2) = &quot; &quot; Then

If Mid(dataline, 30, 2) <> &quot; &quot; Then
str_as_save = str_btn & &quot;,&quot; & str_as_1 & &quot;,&quot; & str_as_2
'MsgBox str_as_save
Call SaveTextFile(str_export & str_btn & &quot;_acct.csv&quot;, str_as_save)
End If

str_as_1 = Mid(dataline, 30, 4)
str_as_2 = Mid(dataline, 38, 37)

MsgBox &quot;*&quot; & str_as_1 & &quot;*--*&quot; & str_tmp_as_1 & &quot;*&quot;
If str_as_1 = &quot; &quot; And str_tmp_as_1 = &quot; &quot; Then
str_as_2 = str_tmp_as_2 & str_as_2
'MsgBox str_as_2
End If

str_tmp_as_1 = str_as_1
str_tmp_as_2 = str_as_2

End If
End If
End If
End If



'Call SaveTextFile(str_export & str_btn & &quot;_acct.csv&quot;, str_as_save)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''


Loop

Close #filehandle

getout:
Exit Sub
End Sub
 
Here is something that might get started.

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

Public Sub ParseFile()

Dim lInt_InHand As Integer
Dim lInt_OutHand As Integer
Dim lStr_DataLine As String
Dim lVar_LineParts As Variant
Dim lStr_LineType As String
Dim lStr_OutLine As String
Dim lInt_Idx As Integer
Dim lStr_KeyCode As String
Dim lStr_NewLine As String
Dim lBol_NewLine As Boolean

lStr_OutLine = &quot;&quot;
lInt_InHand = FreeFile
Open &quot;C:\vb_projs\LineCount\ParseData.txt&quot; For Input As #lInt_InHand
lInt_OutHand = FreeFile
Open &quot;C:\vb_projs\LineCount\ParseData.lst&quot; For Output As #lInt_OutHand

Do While (EOF(lInt_InHand) = False)
Line Input #lInt_InHand, lStr_DataLine
If (Left(lStr_DataLine, 1) <> &quot; &quot;) Then
If (Len(lStr_OutLine) > 0) Then
Print #lInt_OutHand, lStr_OutLine
lStr_OutLine = &quot;&quot;
End If
lBol_NewLine = True
Else
lBol_NewLine = False
End If
lStr_DataLine = Trim(lStr_DataLine)
lStr_NewLine = Replace(lStr_DataLine, &quot; &quot;, &quot; &quot;)
Do While (lStr_NewLine <> lStr_DataLine)
lStr_DataLine = lStr_NewLine
lStr_NewLine = Replace(lStr_DataLine, &quot; &quot;, &quot; &quot;)
Loop
lVar_LineParts = Split(lStr_DataLine, &quot; &quot;)
If (lBol_NewLine = True) Then
lStr_KeyCode = Trim(lVar_LineParts(3)) & Trim(lVar_LineParts(4))
lInt_Idx = InStr(lStr_KeyCode, &quot;/&quot;)
If (lInt_Idx > 0) Then
lStr_KeyCode = Left(lStr_KeyCode, (lInt_Idx - 1))
End If
lStr_KeyCode = Replace(lStr_KeyCode, &quot;-&quot;, &quot;&quot;)
lStr_OutLine = lStr_KeyCode & &quot;,&quot;
End If
For lInt_Idx = 0 To UBound(lVar_LineParts)
If (Trim(lVar_LineParts(lInt_Idx)) = &quot;T&quot;) Then
lStr_OutLine = Trim(lStr_OutLine) & &quot;,&quot;
Else
If (lBol_NewLine = True) Then
If (lInt_Idx < 2) Then
lStr_OutLine = lStr_OutLine & Trim(lVar_LineParts(lInt_Idx)) & &quot;,&quot;
Else
lStr_OutLine = lStr_OutLine & Trim(lVar_LineParts(lInt_Idx)) & &quot; &quot;
End If
Else
lStr_OutLine = lStr_OutLine & Trim(lVar_LineParts(lInt_Idx)) & &quot; &quot;
End If
End If
Next lInt_Idx
Loop

If (Len(lStr_OutLine) > 0) Then
Print #lInt_OutHand, lStr_OutLine
End If

Close #lInt_InHand
Close #lInt_OutHand

End Sub
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
Another question as some times the text display in the forum can be decieving, is there a tab indent on the lines that follow the &quot;ALN&quot; line. If I am understanding your question that is where you are having a hard time determing how to parse the file, as there may be a varying number of lines to follow, correct? Anything is possible, the problem is I only have one lifetime.
[cheers]
 
the details on the lines following the ALN should start the same numbers of characters from the left as does the the /TN after ALN...
 
CajunCenturionm, that code almost works perfect...the only thing i am having trouble with is modifying the output to look like this:

2123971354,1,ALN,/TN 212 397-1354/LPIC NYC /LPCX 0698/LPCA SN, 1-27-99 /PIC ATX/PICX 0288,.89
2123971354,1,NBJ,/TN 212 397-1354/PRIV Y,1.22
2123971354,1,POR2X,/TN 212 397-1354,3.23
2123971354,1,RTVX6,/TN 212 397-1354/LCC SBM,1.11
2123971405,1,ALN,/TN 212 397-1405/LPIC NYC /LPCX 0698/LPCA SN, 1-27-99 /PIC ATX/PICX 0288 /PCA SN, 1-27-99,.67
2123971405,1,NBJ,/TN 212 397-1405/PRIV Y,0

also i need to place the number following the /TN before each line in relevence to the ALN that defined the /TN

any ideas

thanks for help so far, it has been really usefull in getting me going down the right path

thanks

keith
 
This looks like a classic scenario where Regular Expressions would prove useful. Am away from a box with VB on it aty the moment, so cannot knock together anything illustrative 'til tomorrow.
 
I'm sorry, but I compared you output list with the output generated by the program, and the only difference I found was in the first ALN line.

Unless I missed something, your output left out the
/PCA SN, 1-27-99
segment from the first ALN line. All of the rest looked to me to be the same char by char.

I you can provide more specifics on where the output needs to be adjusted, I'll try and help Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
i thought about using RE but my last experience with them wasnt so nice, though id give it a shot again
 
>my last experience with them wasnt so nice

They do take some getting used to - but once you figure out what they are about, they can prove to be very powerful.
 
CajunCenturion,

how can i account for blank lines in source text file...

thanks again

keith
 
o While (EOF(lInt_InHand) = False)
Line Input #lInt_InHand, lStr_DataLine

If (len(trim(lstr_dataline)) > 0) Then

If (Left(lStr_DataLine, 1) <> &quot; &quot;) Then
If (Len(lStr_OutLine) > 0) Then
Print #lInt_OutHand, lStr_OutLine
lStr_OutLine = &quot;&quot;
End If
lBol_NewLine = True
Else
lBol_NewLine = False
End If
lStr_DataLine = Trim(lStr_DataLine)
lStr_NewLine = Replace(lStr_DataLine, &quot; &quot;, &quot; &quot;)
Do While (lStr_NewLine <> lStr_DataLine)
lStr_DataLine = lStr_NewLine
lStr_NewLine = Replace(lStr_DataLine, &quot; &quot;, &quot; &quot;)
Loop
lVar_LineParts = Split(lStr_DataLine, &quot; &quot;)
If (lBol_NewLine = True) Then
lStr_KeyCode = Trim(lVar_LineParts(3)) & Trim(lVar_LineParts(4))
lInt_Idx = InStr(lStr_KeyCode, &quot;/&quot;)
If (lInt_Idx > 0) Then
lStr_KeyCode = Left(lStr_KeyCode, (lInt_Idx - 1))
End If
lStr_KeyCode = Replace(lStr_KeyCode, &quot;-&quot;, &quot;&quot;)
lStr_OutLine = lStr_KeyCode & &quot;,&quot;
End If
For lInt_Idx = 0 To UBound(lVar_LineParts)
If (Trim(lVar_LineParts(lInt_Idx)) = &quot;T&quot;) Then
lStr_OutLine = Trim(lStr_OutLine) & &quot;,&quot;
Else
If (lBol_NewLine = True) Then
If (lInt_Idx < 2) Then
lStr_OutLine = lStr_OutLine & Trim(lVar_LineParts(lInt_Idx)) & &quot;,&quot;
Else
lStr_OutLine = lStr_OutLine & Trim(lVar_LineParts(lInt_Idx)) & &quot; &quot;
End If
Else
lStr_OutLine = lStr_OutLine & Trim(lVar_LineParts(lInt_Idx)) & &quot; &quot;
End If
End If
Next lInt_Idx

End If

Loop
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
CajunCenturion,

Thanks for your help, it works perfect
 
does anyone have an example of the above solution CajunCenturion supplied using regular expressions. i would like to compare CajunCenturion solutions with a regular expressions soltion

any help will be greatly appreciated

thanks

keith
 
Regular Expressions: if I get the time, I'll try and knock something up.
 
thanks a million...i dont have too much experience using regular expressions and any help would be greatly appreciated...i just got some docs from msdn but i need a applicable example to really get a grasp on it

thanks

keith
 
Almost done - but got to go rehearse a play, so I won't be posting the RE example for at least a few hours.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top