How to merge 2 text files?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Hamlet
Posts: 32
Joined: 02 Oct 2013, 09:55
Location: Seoul, Korea

Re: How to merge 2 text files?

15 May 2014, 01:02

This is a simple comparison.

Code: Select all

;								1 Million	 				 	2 Million
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Preparing a long string		    6 Sec 	   					18 Sec 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	Simple_Sort					+	1/2 Sec					+   1 Sec            
;	Simple_Object				+     4 Sec					+   8 Sec
;	RegEx_Loop				    +     5 Sec					+ 11 Sec 
;	COM_Object					+     6 Sec					+ 16 Sec
;	OverKill_Function			+    22 Sec					+ 52 Sec

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	Simple_Sort
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LoL  :=  A_TickCount
Loop, 2000000												
	a  .=  "bb`ncc`naa`n"
Sort, a, U
MsgBox % a  "`n`n"
.	( A_TickCount - LoL ) //1000  "  Sec"		
Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	Simple_Object
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LoL  :=  A_TickCount
Loop, 2000000														
	a  .=  "bb`ncc`naa`n"
b  :=  StrSplit( a, "`n" )
c  :=  {}
For i  in  b
	c.Insert( b[i], "")
For i  in  c
	d  .= i  "`n"
MsgBox % d  "`n`n"
.	( A_TickCount - LoL ) //1000  "  Sec"		
Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	RegEx_Loop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LoL  :=  A_TickCount
Loop, 2000000
	a  .=  "bb`ncc`naa`n"
Loop, Parse, a, `n
	a  :=  RegExReplace( a, A_LoopField "`n", "", "",, RegExMatch( a, A_LoopField ) + StrLen( A_LoopField ))
Msgbox %  RegExReplace( a, "`n`n" )  "`n`n"
.	( A_TickCount - LoL ) //1000  "  Sec"	
Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	COM_Object
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LoL  :=  A_TickCount
Loop, 2000000													
	a  .=  "bb`ncc`naa`n"
b  :=  StrSplit( a, "`n" )
c  :=  ComObjCreate( "Scripting.Dictionary" )
For  i  in  b
	 c.Item( b[i] )
For  i  in  c
	 d  .=  i  "`n"
MsgBox % d  "`n`n"
.	( A_TickCount - LoL ) //1000  "  Sec"		 	
Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	OverKill_Function		;   Thanks for your efforts "FanaticGuru"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LoL  :=  A_TickCount
Loop, 2000000																
	a  .=  "bb`ncc`naa`n"
MsgBox % RemoveDuplicate( a, "`n")  "`n`n"
.	( A_TickCount - LoL ) //1000  "  Sec"		
Return
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to merge 2 text files?

15 May 2014, 02:48

If you are looking for speed and simplicity of code then Parse is better:

Code: Select all

c  :=  ComObjCreate( "Scripting.Dictionary" )
Loop, Parse, a, `n
	if !c.Item(A_LoopField)
		c.Item(A_LoopField) := true, d .= "`n" A_LoopField
Here is a benchmark script to test it:

Code: Select all

; COM_Object Parse
Loop, 1000000
	a  .=  "bb`ncc`naa`n"
Start := A_TickCount
;===== Start Timing Duplicate Remove
c  :=  ComObjCreate( "Scripting.Dictionary" )
Loop, Parse, a, `n
	if !c.Item(A_LoopField)
		c.Item(A_LoopField) := true, d .= "`n" A_LoopField
;===== End Timing Duplicate Remove
Stop := A_TickCount
Lapse := (Stop-Start)/1000 "Seconds"
MsgBox % "COM_Oject Parse`n" d "`n`n" Lapse
a=
b=
c=
d=

; COM_Object
Loop, 1000000
	a  .=  "bb`ncc`naa`n"
Start := A_TickCount
;===== Start Timing Duplicate Remove
b  :=  StrSplit( a, "`n" )
c  :=  ComObjCreate( "Scripting.Dictionary" )
For  i  in  b
	c.Item( b[i] )
For  i  in  c
	d  .=  i  "`n"
;===== End Timing Duplicate Remove
Stop := A_TickCount
Lapse := (Stop-Start)/1000 "Seconds"
MsgBox % "COM_Object`n" d "`n`n" Lapse
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Hamlet
Posts: 32
Joined: 02 Oct 2013, 09:55
Location: Seoul, Korea

Re: How to merge 2 text files?

15 May 2014, 03:54

Thanks, FanaticGuru
The relativeness is more important than the absolute values (in this kind of time consuming testing).
(Am I right ? whatever...)

I changed the long_string.
This time, tried to make more RANDOM ones.
And found somewhat diffirent from the first testing.

Changed part:

Code: Select all

;	Before
;	a  .=  "bb`ncc`naa`n"

;	After
;	a  .=  R() R()


; So, like tihis
Loop, 1000000
	a  .=  R() R()
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
R()
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
	Random, L, 65, 90
	Random, n, 0, 9
	Random, s, 97, 122
	Return  %  Chr(L)  n  Chr(s)  "`n"
}
And, Results are;

Code: Select all

;						        1 Million	 				 	 2 Million			    4 Million	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Preparing a long string		 10 Sec 	   					25 Sec 				    80 Sec
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	Simple_Sort					+	  2 Sec				    +   5 Sec           		+	11 Sec
;	Simple_Object				+     4 Sec					+   7 Sec					+	14 Sec
;	COM_Object					+     6 Sec					+  12 Sec                	+	25 Sec
;	OverKill_Function			+    18 Sec					+  34 Sec               	+   67 Sec

;	RegEx_Loop	(0.1 Million)     30 Sec
;				(0.5 Million)    155 Sec
;				(   1 Million)   LoL Sec
Hamlet
Posts: 32
Joined: 02 Oct 2013, 09:55
Location: Seoul, Korea

Re: How to merge 2 text files?

15 May 2014, 03:56

> FanaticGuru
If I have some more time to testing (some time later), I will consider your suggestion.
Not, today.

Regards..
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: How to merge 2 text files?

15 May 2014, 06:05

I suggest QPC/QPX from L1 :)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
Hamlet
Posts: 32
Joined: 02 Oct 2013, 09:55
Location: Seoul, Korea

Re: How to merge 2 text files?

15 May 2014, 08:18

I just found it.
http://www.autohotkey.com/board/topic/9 ... /?p=579284

Will have a look at it with leisure.


Thanks, Good Info.
Hamlet
Posts: 32
Joined: 02 Oct 2013, 09:55
Location: Seoul, Korea

Re: How to merge 2 text files?

15 May 2014, 09:36

Code: Select all

;                                1 Million                          2 Million              4 Million  
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Preparing a long string        10 Sec                                       
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;   RegEx_Loop               +  335 Sec 
*** For this code, I Used the "QPX" of L1's. It works fine with me. And gave me average 345 Sec. It is nice. Thanks L1
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: How to merge 2 text files?

15 May 2014, 16:20

I had used QPX function by SKAN but this is the first time I tried Learning One's QPX class script. It is pretty nice.

Here are my two test functions setup for QPX:

Code: Select all

Loop, 1000000
    List  .=  R() R()
    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
R()
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
    Random, L, 65, 90
    Random, n, 0, 9
    Random, s, 97, 122
    Return  %  Chr(L)  n  Chr(s)  "`n"
}

QPX.Add("COM_Object", List)	; adds ReverseList1 function to a test collection and passes List as parameter to it.
QPX.Add("COM_Object_Parse", List)	; adds ReverseList2 function to a test collection and passes List as parameter to it.
QPX.Test(30)						; starts test and displays results in a window. Left edit = Summary. Right edit = Details.
return

Esc::ExitApp

#Include QPX.ahk	; by Learning one

COM_Object(a)
{
	b  :=  StrSplit( a, "`n" )
	c  :=  ComObjCreate( "Scripting.Dictionary" )
	For  i  in  b
		c.Item( b[i] )
	For  i  in  c
		d  .=  i  "`n"
	return d
}

COM_Object_Parse(a)
{
	c  :=  ComObjCreate( "Scripting.Dictionary" )
	Loop, Parse, a, `n
		if !c.Item(A_LoopField)
			c.Item(A_LoopField) := true, d .= "`n" A_LoopField
	return d
}
Here are the QPX results:

Code: Select all

COM_Object
   5.267860  122%
COM_Object_Parse <<
   4.334700  100%

COM_Object
Min: 4.648446   88%
Ave: 5.267860  100%
Max: 5.865363  111%
Tot: 158.035799

COM_Object_Parse
Min: 3.871624   89%
Ave: 4.334700  100%
Max: 4.746751  110%
Tot: 130.041004
The one using Parse is significantly faster.

As far as I know it is the fastest way to do it short of using something like Mcode.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Hamlet
Posts: 32
Joined: 02 Oct 2013, 09:55
Location: Seoul, Korea

Re: How to merge 2 text files?

15 May 2014, 20:45

Yes, it has meaningful differences.
Thanks again.

I had enough for this.
Usually, it is fairly easy to go to 95% completion point of an coding.
But, after that, I have to pour 95% of my energy to go to the last 4.99%.

Regards

Code: Select all

;	COM_Object_With_Parse <<
;	   4.569841  100%
;	COM_Object_With_Parse_R1
;	   4.651507  102%
;	COM_Object
;	   5.948793  130%
;	COM_Object_R1
;	   6.197713  136%
;	COM_Object_R2
;	   6.153293  135%

Code: Select all

Loop, 1000000
	L  .=  R() R()

QPX.Add( "COM_Object_With_Parse"	, L )
QPX.Add( "COM_Object_With_Parse_R1"	, L )
QPX.Add( "COM_Object"				, L )
QPX.Add( "COM_Object_R1"			, L )
QPX.Add( "COM_Object_R2"			, L )
QPX.Test( 30 )	
	
	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
COM_Object_With_Parse( a )			; "FanaticGuru"'s suggestions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
	c  :=  ComObjCreate( "Scripting.Dictionary" )
	Loop, Parse, a, `n
		If  Not  c.Item( A_LoopField )
			c.Item( A_LoopField )  :=  true, d  .=  "`n" A_LoopField
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
COM_Object_With_Parse_R1( a )			; "FanaticGuru"'s suggestions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
	c  :=  ComObjCreate( "Scripting.Dictionary" )
	Loop, Parse, a, `n
		If  Not  c.Exists( A_LoopField )	
			c.Add( A_LoopField, ""),  d  .=  "`n"  A_LoopField
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
COM_Object( a )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
	b  :=  StrSplit( a, "`n" )
	c  :=  ComObjCreate( "Scripting.Dictionary" )
	For  i  in  b
		c.Item( b[i] )
	For  i  in  c
		d  .=  i  "`n"
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
COM_Object_R1( a )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
	b  :=  StrSplit( a, "`n" )
	c  :=  ComObjCreate( "Scripting.Dictionary" )
	For  i  in  b
		If  Not  c.Exists( b[i] )	
			c.Add( b[i], "")
	For  i  in  c
		d  .=  i  "`n"
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
COM_Object_R2( a )
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
	b  :=  StrSplit( a, "`n" )
	c  :=  ComObjCreate( "Scripting.Dictionary" )
	For  i  in  b
		If  Not  c.Exists( b[i] )	
			c.Add( b[i], ""),  d  .=  b[i]  "`n"
}

*** EDIT ***
One Weird thing.
In my wild guess, the "Exists" method should be faster than "Item" property.
But, the result has its own voice. I do not understand it properly.
Last edited by Hamlet on 16 May 2014, 08:41, edited 1 time in total.
garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: How to merge 2 text files?

16 May 2014, 06:32

just short ideas

Code: Select all

/*
all strings must appear only once in the new file and
the new file must include all unique strings in file1 and file2
*/

;--- example-1 from user AlphaBravo 

F1=%A_scriptdir%\F1.txt    ;-- File1
F2=%A_scriptdir%\F2.txt    ;-- File2
F4=%A_scriptdir%\New.txt   ;-- NEW

ifexist,%f4%
  filedelete,%f4%
FileRead, F1,%F1%
FileRead, F2,%F2%
F8=%F1%%F2%
Sort, F8, U
FileAppend,%f8%,%F4%
run,%f4%
return
;==========================================================


;-- example-2 DOS-command FC FileCompare ----------------

F1=%A_scriptdir%\F1.txt    ;-- File1
F2=%A_scriptdir%\F2.txt    ;-- File2
F3=%A_scriptdir%\F3.txt    ;-- same as File1

;runwait, %comspec% /c  fc /T /N "%f1%" "%f3%",,hide useerrorlevel   ;-- OK-FileCompare nothing found
runwait, %comspec% /c  fc /T /N "%f1%" "%f2%",,hide useerrorlevel    ;-- changed
;runwait, %comspec% /c  fc /T /N "%f1%" "%f4%",,hide useerrorlevel   ;-- file not exist

if (errorlevel=0)
   msgbox,Errorlevel=%errorlevel%`nOK `nBoth files are same
if (errorlevel=1)
   msgbox,Errorlevel=%errorlevel%`nNOT OK`nChanged -Files are different
if (errorlevel=2)
   msgbox,Errorlevel=%errorlevel%`nNOT OK`nFile not exist
return
;==========================================================

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], marypoppins_1, ShatterCoder and 137 guests