#212315 - 2017-03-02 11:09 PM
Parsing 2 files selecting output
|
Sweeny
Fresh Scripter
Registered: 2016-03-18
Posts: 18
Loc: Hampshire
|
I'm having a hard time trying to get the concept of a bit of code to work. Presently I have 2 files, for the sake of ease, File A and File B. File A contains headings then information below in regards to the heading, for example:
1,a heading, some more information Stuff stuff stuff Some more stuff more stuff 2, a second heading, even more information Stuff stuff
File B contains lines in pairs, one line of the pair is the headings found in File A, the second line is information linked to the first. I have written (in hast mind you) a small script to search File A and B. If the script finds a matching heading in both files I'd like it to write out the pair of accossiated lines in File B followed by the accossiated information in File A, only write out the lines in File A up until the next heading. This should loop through until the end of File A is reached. Not having had much success I ask for some help with this approach Many thanks in advance, Tom
|
Top
|
|
|
|
#212316 - 2017-03-02 11:58 PM
Re: Parsing 2 files selecting output
[Re: Sweeny]
|
Glenn Barnas
KiX Supporter
Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
|
Here's some pseudo-ish code, just whipped together/untested:; Load both files into arrays
$aFileA = FileIO('fileA.ext', 'R')
$aFileB = FileIO('fileB.ext', 'R')
; Enumerate File A
For Each $Line in $aFileA
; Detect when the line is a header - look for something that identifies a header
If InStr($Line, "header stuff:") ; found a header
; might need to split off the header part?
$Header = Split($Line, ':')[0] ; assumes that the headers looks like "1, Header stuff: other stuff...) - take everything up to the ':'
;Look in the other file for a line containing the same header
$Target = AScan($aFileB, $Header) ; check args here - might want a partial match
If $Target <> -1 ; was found!
; $Target is the line number in File B's array - read that and extract data until the next header is found
For $P = $Target to $Target + 999 ; scan next 999 lines or till we find the next header
If $P > $Target And InStr($aFileB[$P], 'Header stuff') ; found the NEXT header
$P = $Target + 1000 ; terminate the loop
Else
$aFileB[$P] ? ; output for testing, manipulate if necessary
; This line from File B has data for a matching header. You may need to
; split the File B header ($P = $Target means this is the header line in File B)
EndIf
Next
EndIf
EndIf
Next The FileIO reads a file into an array, can also write an array to a file. Download from the UDF library on my web site (see sig).
Glenn
_________________________
Actually I am a Rocket Scientist!
|
Top
|
|
|
|
#212324 - 2017-03-08 03:19 PM
Re: Parsing 2 files selecting output
[Re: Glenn Barnas]
|
Sweeny
Fresh Scripter
Registered: 2016-03-18
Posts: 18
Loc: Hampshire
|
Glenn that is perfect, after a little bit of bug testing and tweaking its working without a hitch. Cheers, Tom
|
Top
|
|
|
|
#212359 - 2017-03-22 11:35 AM
Re: Parsing 2 files selecting output
[Re: Glenn Barnas]
|
Sweeny
Fresh Scripter
Registered: 2016-03-18
Posts: 18
Loc: Hampshire
|
Hi Glenn, Here is a snippet of my code where it was used.
For Each $Line in $aFileA
If InStr($Line, ",Name,")
writeline($handle2,@crlf + $Line)
endif
Select
Case InStr($Line, "wibble") <> 0
$Target = AScan($Catfiltarray, $Line,,,1) ; Argument tells script to look for partial match
If $Target <> -1 ; was found!
; $Target is the line number in File B's array - read that and extract data until the next header is found
For $P = $Target to $Target + 999 ; scan next 999 lines or till we find the next header
If $P > $Target And InStr($Catfiltarray[$P], ',Name,') ; found the NEXT header
$P = $Target + 1000 ; terminate the loop
Else
Select
case InStr(substr($Catfiltarray[$P],2,9), "Category") ;searching for a line conatining the string Category
$Catfiltarray[$P] = trim(substr($Catfiltarray[$P], 10)) ;some sorting to make results more readable
$Catfiltarray[$P + 1] = trim(substr($Catfiltarray[$P + 1], 9))
writeline($handle2,$Catfiltarray[$P] + "," + $Catfiltarray[$P + 1] + @crlf)
case InStr($Catfiltarray[$P], ",Name,")
$Catfiltarray[$P] = trim(substr($Catfiltarray[$P], 8))
$comma = trim(substr($Catfiltarray[$P],1,1))
if $comma = "," ;If statement compensating for more than 10 catagories present in websense
$Catfiltarray[$P] = trim(substr($Catfiltarray[$P], 9)) ;Will start trimming from 9 characters instead of 8, compensate for 2 character numbers
endif
writeline($handle2,@crlf + ",Category - ," + $Catfiltarray[$P] + @crlf)
EndSelect
EndIf
Next
EndIf
Case InStr($Line, "wibble2") <> 0
$Target = AScan($limfilarray, $Line,,,1) ; Argument tells script to look for partial match
If $Target <> -1
For $P = $Target to $Target + 999 ; scan next 999 lines or till we find the next header
If $P > $Target And InStr($limfilarray[$P], ',,')
$x = writeline($handle2,@crlf)
$P = $Target + 1000
Else
If Instr($limfilarray[$P], "http")
$x = writeline($handle2,@crlf + ",Filter" + $limfilarray[$P])
endif
EndIf
Next
EndIf
EndSelect
Next Please forgive any bad practices it was a rather time constrained write! Cheers, Tom
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 507 anonymous users online.
|
|
|