#125688 - 2004-08-26 08:49 AM
Listing textfile
|
Transalp
Lurker
Registered: 2004-08-26
Posts: 4
|
Hello,
I'm having a few files with textlines. Some of the lines are the same in these files. I need to have a new file with each line only 1 time.
A little example:
textfile1.txt
-------------
string1
string2
string3
textfile2.txt
-------------
string3
string4
string5
textfile3.txt
-------------
string2
string4
result.txt
----------
string1
string2
string3
string4
string5
Could someone please help me?
Edited by Transalp (2004-08-26 09:30 AM)
|
|
Top
|
|
|
|
#125689 - 2004-08-26 09:07 AM
Re: Listing textfile
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Approximately how many unique lines in total will there be?
If it's no more than (say) 10,000 you can load each line into an array. Search the array for the string, if it's not present add it and write to your new file otherwise ignore it.
You can do exactly the same thing with ReadProfileString() and WriteProfileString(), and these are usually easier for starters to understand.
If you have a lot of uniques you may get into memory trouble with arrays.
In this case:
- Catenate all the files together using DOS COPY (the "+" switch)
- User DOS SORT to sort the catenated file.
- Read each line in your sorted file. If it is the same as the last line ignore it, otherwise write it to your new file.
|
|
Top
|
|
|
|
#125690 - 2004-08-26 09:25 AM
Re: Listing textfile
|
Transalp
Lurker
Registered: 2004-08-26
Posts: 4
|
Quote:
Approximately how many unique lines in total will there be?
If it's no more than (say) 10,000 you can load each line into an array. Search the array for the string, if it's not present add it and write to your new file otherwise ignore it.
Let's say 100 lines max.
|
|
Top
|
|
|
|
#125691 - 2004-08-26 09:42 AM
Re: Listing textfile
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
That makes it simpler.
You know you have a pretty small data set, so
- Dimension your array up front, say 1000 elements.
- Read each file in turn, and for each line read...
- If the line exists in the array then ignore it.
- If the line does not exist, add it to the array and write it to your output file.
Job done, easy eh?
Look up information on the following commands in the manual: DIM, Dir(), Open(), ReadLine(), WriteLine(), Ascan().
Have a go, and post again if you start to struggle.
|
|
Top
|
|
|
|
#125693 - 2004-08-26 02:17 PM
Re: Listing textfile
|
maciep
Korg Regular
   
Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
|
And here's a slightly simpler solution
Code:
break on
dim $Out, $FileArray, $file dim $outArray, $line $Out = @scriptdir + "\result.txt" $FileArray = @scriptdir + "\file1.txt",@scriptdir + "\file2.txt",@scriptdir + "\file3.txt"
for each $file in $FileArray if open(1,$file) = 0 $line = readline(1) while @error = 0 if ubound($outArray) < 0 redim $outArray[0] $outArray[0] = $line else if ascan($outArray,$line) < 0 redim preserve $outArray[ubound($outArray)+1] $outArray[ubound($outArray)] = $line endif endif $line = readline(1) loop $ = close(1) endif next
if open(1,$Out,5) = 0 for each $line in $outArray $ = writeline(1, $line + @crlf) next $ = close(1) endif
_________________________
Eric
|
|
Top
|
|
|
|
#125695 - 2004-08-26 03:11 PM
Re: Listing textfile
|
Lonkero
KiX Master Guru
   
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
well, I was thinking more like this:
Code:
break on
dim $Out, $FileArray, $file
dim $outArray, $line
$Out = @scriptdir + "\result.txt"
$FileArray = @scriptdir + "\file1.txt",@scriptdir + "\file2.txt",@scriptdir + "\file3.txt"
if open(2,$Out,5) "failed to open output file, quitting" quit endif
for each $file in $FileArray
if open(1,$file) = 0
$line = readline(1)
while @error = 0
if ascan($outArray,$line) < 0
redim preserve $outArray[ubound($outArray)+1]
$outArray[ubound($outArray)] = $line
$ = writeline(2, $line + @crlf)
endif
endif
$line = readline(1)
loop
$ = close(1)
endif
next
$ = close(2)
_________________________
!download KiXnet
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Henriques)
and 2516 anonymous users online.
|
|
|