hi everbody,
as you see a picture, i have two text files. 1.txt and 2. txt each file has some number.i want to compare these two txt files..for example
1.txt line 1 compare with all line on 2.txt if its match write 3.txt if doesnt find same number its goes to second line on 1.txt.and its continue after all line finished...
compare 2 txt files ?
Moderators: Dorian (MJT support), JRL
- Marcus Tettmar
- Site Admin
- Posts: 7391
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
This does what you ask:
Code: Select all
Let>path=c:\temp
//read both files into memory
ReadFile>%path%\1.txt,file1
ReadFIle>%path%\2.txt,file2
//split file 1 into an array of lines we can loop through
Separate>file1,CRLF,lines
//if lines exist continue
If>lines_count>0
//loop through each line in file1
Let>k=0
Repeat>k
Let>k=k+1
Let>this_line=lines_%k%
//Use a regular expression pattern to see if this line exists somewhere in file 2
RegEx>(?m)^%this_line%$,file2,0,matches,nm,0
//if it exists write this value to file 3
If>nm>0
WriteLn>%path%\3.txt,wlnr,this_line
Endif
Until>k=lines_count
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Marcus Tettmar
- Site Admin
- Posts: 7391
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Just change "Until>k=lines_count" to "Until>k=6"
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Marcus Tettmar
- Site Admin
- Posts: 7391
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Sorry, I misunderstood, that will look at the first 6 lines. I see you mean the first 6 chars of EACH line. Hang on a sec.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
- Marcus Tettmar
- Site Admin
- Posts: 7391
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Here you go. This version looks at the first 6 chars of each line and the regex is modified so that it looks for any lines which start with those digits and are numeric:
Code: Select all
Let>path=c:\temp
//read both files into memory
ReadFile>%path%\1.txt,file1
ReadFIle>%path%\2.txt,file2
//split file 1 into an array of lines we can loop through
Separate>file1,CRLF,lines
//if lines exist continue
If>lines_count>0
//loop through each line in file1
Let>k=0
Repeat>k
Let>k=k+1
Let>this_line=lines_%k%
Let>this_line={Copy(%this_line%,1,6)}
//Use a regular expression pattern to see if this line exists somewhere in file 2
RegEx>(?m)^%this_line%\d*$,file2,0,matches,nm,0
//if it exists write this value to file 3
If>nm>0
WriteLn>%path%\3.txt,wlnr,this_line
Endif
Until>k=lines_count
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Alter this portion:
Of Marcus' samples above.
To be:
Code: Select all
//if it exists write this value to file 3
If>nm>0
WriteLn>%path%\3.txt,wlnr,this_line
Endif
To be:
Code: Select all
//if it exists write this value to file 3
//if it doesn't exist write this value to file 4
If>nm>0
WriteLn>%path%\3.txt,wlnr,this_line
Else
WriteLn>%path%\4.txt,wlnr,this_line
Endif