AutoHotkey Community

It is currently May 27th, 2012, 12:06 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: February 15th, 2011, 1:40 am 
there are five textfiles. how to append first line of each textfile first to a final textfile (final.txt) , then the second lines of the all the five textfiles to final.txt, then the third lines and so on untill all the lines of the textfiles are appended to final.txt ...
in the end the final.txt will contain all the content in the five textfiles


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 15th, 2011, 4:01 am 
http://www.autohotkey.com/docs/commands ... adLine.htm

http://www.autohotkey.com/docs/commands/FileAppend.htm

examples at the bottom should lead you in the right direction


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 15th, 2011, 4:03 am 
Offline

Joined: December 8th, 2006, 5:17 am
Posts: 248
Location: Sydney Australia
ugly but it seems to work. I am sure others with better skills could improve it.

Code:
;delete the file
filedelete final.txt

;###################################################################

; below is the 'natural sorting' function
; from http://www.autohotkey.com/forum/viewtopic.php?t=35835

_LogicalSort(x,y,offset)
{
    static RegEx := "S)(\D*)(\d*)(.*)"
   
    Loop
    {
        RegExMatch(x, RegEx, x)
        RegExMatch(y, RegEx, y)
       
        if (x1 != y1)
            return x1 < y1 ? -1 : 1
           
        if (x2 != y2)
            return x2 < y2 ? -1 : 1
           
        if (x3 = "" || y3 = "")
            break
           
        x := x3
        y := y3
    }

    if (x3 != "")
    {
        ;x is "longer"
        return 1
    }
    else if (y3 != "")
    {
        ;y is "longer"
        return -1
    }
    else
    {
        ;equal - maintain original order (stable)
        return -offset
    }
}

; read each file and append to final file. add a number + "_" to each read line for sorting
Loop, Read, file1.txt , final.txt
   {
   fileappend, %a_index%_%A_LoopReadLine%`n,
   }
Loop, Read, file2.txt , final.txt
   {
   fileappend, %a_index%_%A_LoopReadLine%`n,
   }
Loop, Read, file3.txt , final.txt
   {
   fileappend, %a_index%_%A_LoopReadLine%`n,
   }
Loop, Read, file4.txt , final.txt
   {
   fileappend, %a_index%_%A_LoopReadLine%`n,
   }
Loop, Read, file5.txt , final.txt
   {
   fileappend, %a_index%_%A_LoopReadLine%`n,
   }

; read the final file
FileRead, final_var, final.txt

; destroy the final file
filedelete final.txt

; sort the variable from fileread
sort, final_var,F_LogicalSort

; loop thru and strip out the number + "_" at the start of each line
loop, parse, final_var, `n, `r
   {
   sorted .= RegExReplace(a_loopfield,"^(\d+_)(.*)","$2") . "`n"
   }

;recreate the final file
fileappend, %sorted%, final.txt
msgbox finished




_________________
Paul O


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 15th, 2011, 4:27 am 
Offline

Joined: April 8th, 2009, 7:49 pm
Posts: 6074
Location: San Diego, California
Can you give an approximate length of the 5 files?

I ask because there are at least two ways besides poo_noo's example, but there are tradeoffs of speed vs file size.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 15th, 2011, 9:02 am 
The lenght of all the files is random ,could be anything


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 15th, 2011, 9:29 am 
Offline

Joined: June 4th, 2010, 9:04 pm
Posts: 1347
Location: california
Ok if I read this right you are looking to interleave the lines from each file with the lines from all the other files. Question: if the files can be of any length, how do you wish to handle the situation where one file runs out of lines before the other files? If there is supposed to be a true correlation between the lines of one file and the lines of the other files, how do you distinguish when one file runs out and that interleave correlation stops and becomes invalid? Seems that would throw off any means of knowing which lines came from which files, unless of course you retain the original files for analysis. Given that scenario, why not just append all the files one at a time without interleaving, and flag the end of each file so you know when each one stops? To get a true interleave, the files would all have to be the same length. Is this a characteristic of the files you intend to merge?

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 15th, 2011, 10:50 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Using http://www.autohotkey.net/~hugov/tf-lib.htm#TF_ConCat you could merge all the lines side by side, if you include say 500 spaces or something like that as a delimiter you can simply replace those 500 with a new line char at the end so all lines 1 will be beneath each other.

Code:
;#include tf.AutoHotkey ; uncomment this line if you don't have TF in your LIB folder
TF_ConCat("1.txt", "2.txt", "temp.txt", 500)
TF_ConCat("temp.txt", "3.txt", "temp.txt", 500)
TF_ConCat("temp.txt", "4.txt", "temp.txt", 500)
TF_ConCat("temp.txt", "5.txt", "temp.txt", 500)
Loop 500
   Find .= A_Space
TF_Replace("!temp.txt", Find, "`r`n")

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group