Technical support and scripting issues
Moderators: JRL, Dorian (MJT support)
-
carkons
- Newbie
- Posts: 16
- Joined: Wed Jul 27, 2011 12:33 am
Post
by carkons » Thu Sep 15, 2011 11:02 pm
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...

-
Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
-
Contact:
Post
by Marcus Tettmar » Fri Sep 16, 2011 7:34 am
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
-
carkons
- Newbie
- Posts: 16
- Joined: Wed Jul 27, 2011 12:33 am
Post
by carkons » Fri Sep 16, 2011 7:58 am
thank you very much for answer its work perfect.
i want to ask one more question for same issue.what about if i want to compare first 6 number ?
i mean not all number in line ,
3248324829 - - - - -3248327432
for if first 6 number match write 3.txt..
-
Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
-
Contact:
Post
by Marcus Tettmar » Fri Sep 16, 2011 8:01 am
Just change "Until>k=lines_count" to "Until>k=6"
-
carkons
- Newbie
- Posts: 16
- Joined: Wed Jul 27, 2011 12:33 am
Post
by carkons » Fri Sep 16, 2011 8:03 am
thanks ..trying now
-
Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
-
Contact:
Post
by Marcus Tettmar » Fri Sep 16, 2011 8:07 am
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
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
-
Contact:
Post
by Marcus Tettmar » Fri Sep 16, 2011 8:09 am
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
-
newuser
- Pro Scripter
- Posts: 64
- Joined: Tue Jun 11, 2013 4:53 pm
Post
by newuser » Thu Jul 11, 2013 1:45 pm
This is nice but can anyone show me an example how to compare two files and write the unmatch lines to a third file?
Thanks.
-
JRL
- Automation Wizard
- Posts: 3530
- Joined: Mon Jan 10, 2005 6:22 pm
- Location: Iowa
Post
by JRL » Thu Jul 11, 2013 6:12 pm
Alter this portion:
Code: Select all
//if it exists write this value to file 3
If>nm>0
WriteLn>%path%\3.txt,wlnr,this_line
Endif
Of Marcus' samples above.
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
-
newuser
- Pro Scripter
- Posts: 64
- Joined: Tue Jun 11, 2013 4:53 pm
Post
by newuser » Sat Jul 13, 2013 5:19 pm
Thanks and to merge both these files, use code below.
[code]
AppendFile>%path%\3.txt,%path%\4.txt,%path%\newfile.txt,result
[/code]