RE: [Clip] Merging two files
- Thank you, Flo and Art! I'm on another task right now, but will get back to this in a day or two.
From: firstname.lastname@example.org [mailto:email@example.com] On Behalf Of flo.gehrke
Sent: Saturday, August 27, 2011 13:56
Subject: Re: [Clip] Merging two files
--- In firstname.lastname@example.org <mailto:ntb-clips%40yahoogroups.com> , "John Shotsky" <jshotsky@...> wrote:
> I want a general purpose method, because there are different
> cases of what needs to be done. In some cases, each line in
> file1 and file2 start with the same words, which facilitates
> the match. In other cases, there will be two files with
> simply the same number of lines, but I want the same process to
> occur � the contents of file2 appear on the ends of each
> File1 line, but without matching words in file2.
If the two files should be combined 'line by line' like...
file1 line #1 + file2 line #1
file1 line #2 + file2 line #2
I think this is perfectly achieved with Art's idea (message #22020) according with your sample.
Completing Art's proposal and providing that both File1 and File2 contain the same amount of lines you could try...
^!If ^%i% > ^%Left0% Out
^!AppendtoFile ^$GetDocumentPath$NewFile.TXT ^%Left^%i%% ^%Right^%i%%^%NL%
; In case you want to replace the original File1...
^!RenameFile ^$GetDocumentPath$NewFile.TXT ^$GetDocumentPath$File1.txt
If you want to see the result in a new document before saving it to the disk you might prefer the following clip. It
also avoids empty lines...
; Strip empty records from end of array (by courtesy of Eb)
^!If "^%Left^%Left0%%" <> "^%Empty%" Skip Else ^!Dec %Left0%
^!If ^%i% > ^%Left0% End
^!InsertText ^%Left^%i%% ^%Right^%i%%^%NL%
If the result is correct save it as File1.TXT replacing the original file.
[Non-text portions of this message have been removed]
If the files are large you may want to look at "join" a command line
tool from GnuWin32 CoreUtils. It does what you want. I'd construce the
command line in NT and let join do the work.
Art's array is good too. You could parse the File2 to split on space so
that you don't add the unique number again.
; set arrays as per Art's example. Rename array names so it does not end
in a number.
; Use the "loop element" from clip assistant
^!If ^%Index% > ^%LoopCount% EndLoop
; add safety line to skip empty lines usually at the end of a file.
^!if "^%File1^%index%%"="" LoopStart
; set up to parse the second file after the unique number. Assumes only
one space in lines in file2
^!setlistdelimiter " "
^!AppendtoFile "^%newFile%" ^%File1^%index%% ^%linedata2%^%nl%
On 26/08/2011 4:27 AM, John Shotsky wrote:
> I'm looking for a standard way to add the contents of a file (file2) to the ends of the matching lines of another file.
> Both files have the same number of lines, and I can begin each line with a number, such as 1., 2., if needed. Or they
> can both start with a unique text name. There will never be duplicate lines within either file.
> I have been doing this by copying the contents of file2 to the bottom of file1, then using a replace statement to
> replace the first line with itself plus the desired contents of the lower matching line, which is consumed in the
> process. When no more matches are found, it is done. There is no row counting, etc, just an 'IfError Next Else Skip_-1
> to keep repeating the process until all the lower lines are consumed.
> Is there a more direct way to do this? I'd like to create a general purpose clip that would add any file2 to any file1
> without first copying file2 into file1, then processing them. There will always be a key at the beginning of these lines
> in both files, but otherwise the content of each line will be different. I suspect there is a way to do this with an
> array, but I don't know how to construct that.
> One example of this would be adding page numbers found only in file2 to each matching line in file1.
> Fookes Software: http://www.fookes.com/
> NoteTab website: http://www.notetab.com/
> NoteTab Discussion Lists: http://www.notetab.com/groups.php
> Yahoo! Groups Links