#125094 - 2004-08-16 03:54 PM
Need Script, compare fileA with fileB & write fileB to fileC
|
DMiles
Fresh Scripter
Registered: 2004-08-16
Posts: 10
|
I am Totally new with Kixtart. Need script to read records in FileA (SSN). For each occurence of SSN in FileB, write FileB record in FileC. dick.miles@nortonhealthcare.org
|
|
Top
|
|
|
|
#125095 - 2004-08-16 03:59 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
This is a pretty common requirement, search the board for some examples of how it's been done in the past.
The easiest way is:
- Read entire FileA into an array.
- Read FileB, search array for each record.
- If the record is present, write to FileC
This works much better if the files are sorted.
|
|
Top
|
|
|
|
#125096 - 2004-08-16 05:16 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
DMiles
Fresh Scripter
Registered: 2004-08-16
Posts: 10
|
Have searched, can't find. Can you provide example? Thanks
|
|
Top
|
|
|
|
#125098 - 2004-08-16 05:44 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
DMiles
Fresh Scripter
Registered: 2004-08-16
Posts: 10
|
Version 4.02. File A will have 9 position SSN #. FileB will have records with 9 position SSN# plus other data. I am familiar with Substr to find SSN in FileB. Thanks
|
|
Top
|
|
|
|
#125099 - 2004-08-16 05:45 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
Here is a quick example to help get you started...
Code:
$fileA = loadfile("fileA.txt")
$fileb = loadfile("fileb.txt")
$ = Open(1,"Filec.txt",5)
For Each $line In $fileB
If AScan($filea,$line)+1
? "A line In file b was found In file a, writting the line To file c"
$ = WriteLine(1,$line+@CRLF)
EndIf
Next
Function loadfile($file)
DIM $fso,$f,$fs
$fso = CreateObject("Scripting.FileSystemObject")
$f = $fso.GetFile($file)
If @ERROR Exit(2) EndIf
$fs = $f.OpenAsTextStream(1)
$loadfile = Split($fs.Read($f.size),@CRLF)
Exit(@ERROR)
EndFunction
Edited by Bryce (2004-08-16 05:47 PM)
|
|
Top
|
|
|
|
#125100 - 2004-08-16 05:48 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
DMiles
Fresh Scripter
Registered: 2004-08-16
Posts: 10
|
Thanks Bryce, i will give it a shot........
|
|
Top
|
|
|
|
#125101 - 2004-08-16 05:59 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
Quote:
Thanks Bryce, i will give it a shot........
hmm i just read, your post regarding kix version and sample file data...
be sure to isolate the SSN from the $line variable in the for next loop before running the ascan() command.
Making the assumption that fileB data is like the following...
Code:
123-45-6789,smith,bill,1313 mocking bird lane,blahh..,blah,blah
you will need to isolate the SSN first.
if it is comma delimited.... and the SSN is the first data field...
Code:
For Each $line In $fileB
If AScan($filea,split($line,",")[0])+1
? "A SSN In file b was found In file a, writting the line To file c"
$ = WriteLine(1,$line+@CRLF)
EndIf
Next
Bryce
Edited by Bryce (2004-08-16 06:02 PM)
|
|
Top
|
|
|
|
#125102 - 2004-08-16 06:46 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
DMiles
Fresh Scripter
Registered: 2004-08-16
Posts: 10
|
Bryce, here is what I am atempting, but not working for me.
cls
$fileA = loadfile("c:\ssn.txt")
$fileb = loadfile("c:\data.txt")
$filec = Open(1,"c:\outdata.txt",5)
For Each $line In $fileb
$ssn = substr($line,1,9)
If AScan($filea,"$ssn")+1
? "A SSN In file b was found In file a, writting the line To file c"
$ = WriteLine(1,$line+@CRLF)
EndIf
Function loadfile($file)
DIM $fso,$f,$fs
$fso = CreateObject("Scripting.FileSystemObject")
$f = $fso.GetFile($file)
If @ERROR Exit(2) EndIf
$fs = $f.OpenAsTextStream(1)
$loadfile = Split($fs.Read($f.size),@CRLF)
Exit(@ERROR)
EndFunction
Edited by DMiles (2004-08-16 06:47 PM)
|
|
Top
|
|
|
|
#125104 - 2004-08-16 06:59 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
DMiles
Fresh Scripter
Registered: 2004-08-16
Posts: 10
|
Les, removed Quotes. still no work. Does not even create outdata.txt file.
|
|
Top
|
|
|
|
#125106 - 2004-08-16 07:21 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
ShaneEP
MM club member
   
Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
|
Yes I also noticed the missing Next...But it seemed to work fine for me even without it. I did add it below however...The following works fine on my machine. I added the get $ after the end of the loop so that you can see on the console if it does indeed find any matches or not. Other than that just verify that the paths for the $fila, b, and c are correct and you have the necessary rights to create the filec on the C:\.
Code:
cls $fileA = loadfile("c:\ssn.txt") $fileb = loadfile("c:\data.txt") $filec = Open(1,"c:\outdata.txt",5)
For Each $line In $fileb $ssn = substr($line,1,9) If AScan($filea,"$ssn")+1 ? "A SSN In file b was found In file a, writting the line To file c" $ = WriteLine(1,$line+@CRLF) EndIf Next
? "DONE - Press any key to exit" get $
Function loadfile($file) DIM $fso,$f,$fs $fso = CreateObject("Scripting.FileSystemObject") $f = $fso.GetFile($file) If @ERROR Exit(2) EndIf $fs = $f.OpenAsTextStream(1) $loadfile = Split($fs.Read($f.size),@CRLF) Exit(@ERROR) EndFunction
|
|
Top
|
|
|
|
#125108 - 2004-08-16 08:33 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
DMiles
Fresh Scripter
Registered: 2004-08-16
Posts: 10
|
Dummy me. It was creating the Out file. I was just looking for it in wrong place. Now, when I execute script, i am getting:
Script error: expected sxpression If AScan($fileb$ssn,1,9)+1
Here is my code:
cls $fileA = loadfile("c:\ftp\ssn.txt") $fileb = loadfile("c:ftp\data.txt") $filec = Open(1,"c:\ftp\outdata.txt",5)
For Each $line In $filea $ssn = substr($line,1,9)
For Each $line In $filea If AScan($fileb,$ssn,1,9)+1 ? "A SSN In file b was found In file a, writting the line To file c" $ = WriteLine(1,$line+@CRLF) EndIf
Next ? "DONE - Press any key to exit" get $ Function loadfile($file) DIM $fso,$f,$fs $fso = CreateObject("Scripting.FileSystemObject") $f = $fso.GetFile($file) If @ERROR Exit(2) EndIf $fs = $f.OpenAsTextStream(1) $loadfile = Split($fs.Read($f.size),@CRLF) Exit(@ERROR) EndFunction
|
|
Top
|
|
|
|
#125110 - 2004-08-16 08:51 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
DMiles
Fresh Scripter
Registered: 2004-08-16
Posts: 10
|
4.02
|
|
Top
|
|
|
|
#125111 - 2004-08-16 08:54 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
ShaneEP
MM club member
   
Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
|
I think you got an extra For-Next in there for starters...Not sure if the extra parameters are needed in your AScan either since you are already SubStr the SSN...Try replacing this...
Code:
For Each $line In $filea $ssn = substr($line,1,9)
For Each $line In $filea If AScan($fileb,$ssn,1,9)+1 ? "A SSN In file b was found In file a, writting the line To file c" $ = WriteLine(1,$line+@CRLF) EndIf
With this...
Code:
For Each $line In $filea $ssn = substr($line,1,9) If AScan($fileb,$ssn)+1 ? "A SSN In file a was found In file b, writting the line To file c" $ = WriteLine(1,$line+@CRLF) EndIf
|
|
Top
|
|
|
|
#125113 - 2004-08-16 09:01 PM
Re: Need Script, compare fileA with fileB & write fileB to fileC
|
ShaneEP
MM club member
   
Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
|
Oh yea...forgot about the AScan not working until after your version. This should work for you to get around the ascan limitation i believe. There was also a missing '\' in one of the paths in your posted code...So make sure to check that.
Code:
cls $fileA = loadfile("c:\ftp\ssn.txt") $fileb = loadfile("c:\ftp\data.txt") $filec = Open(1,"c:\ftp\outdata.txt",5)
For Each $line In $filea $ssn = substr($line,1,9) For Each $lineb In $fileb If InStr ($lineb,$ssn) ? "A SSN In file b was found In file a, writting the line To file c" $ = WriteLine(1,$line+@CRLF) Endif Next Next
? "DONE - Press any key to exit" get $
Function loadfile($file) DIM $fso,$f,$fs $fso = CreateObject("Scripting.FileSystemObject") $f = $fso.GetFile($file) If @ERROR Exit(2) EndIf $fs = $f.OpenAsTextStream(1) $loadfile = Split($fs.Read($f.size),@CRLF) Exit(@ERROR) EndFunction
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1662 anonymous users online.
|
|
|