Loading ...
Sorry, an error occurred while loading the content.

Re: [Clip] Merging two files

Expand Messages
  • flo.gehrke
    ... John, If the two files should be combined line by line like... file1 line #1 + file2 line #1 file1 line #2 + file2 line #2 etc... I think this is
    Message 1 of 10 , Aug 27, 2011
    • 0 Attachment
      --- In ntb-clips@yahoogroups.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.

      John,

      If the two files should be combined 'line by line' like...

      file1 line #1 + file2 line #1
      file1 line #2 + file2 line #2
      etc...

      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...


      ^!SetListDelimiter ^%NL%
      ^!SetArray %Left%=^$GetFileText(^$GetDocumentPath$File1.TXT)$
      ^!SetArray %Right%=^$GetFileText(^$GetDocumentPath$File2.TXT)$
      ^!Set %i%=0

      :Loop
      ^!Inc %i%
      ^!If ^%i% > ^%Left0% Out
      ^!AppendtoFile ^$GetDocumentPath$NewFile.TXT ^%Left^%i%% ^%Right^%i%%^%NL%
      ^!Goto Loop

      :Out
      ; In case you want to replace the original File1...
      ^!RecycleFile ^$GetDocumentPath$File1.txt
      ^!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...


      ^!SetListDelimiter ^%NL%
      ^!SetArray %Left%=^$GetFileText(^$GetDocumentPath$File1.TXT)$
      ^!SetArray %Right%=^$GetFileText(^$GetDocumentPath$File2.TXT)$
      ; Strip empty records from end of array (by courtesy of Eb)
      ^!If "^%Left^%Left0%%" <> "^%Empty%" Skip Else ^!Dec %Left0%
      ^!Goto Skip_-1
      ^!Set %i%=0

      :Loop
      ^!Inc %i%
      ^!If ^%i% > ^%Left0% End
      ^!InsertText ^%Left^%i%% ^%Right^%i%%^%NL%
      ^!Goto Loop


      If the result is correct save it as File1.TXT replacing the original file.

      Regards,
      Flo
    • John Shotsky
      Thank you, Flo and Art! I m on another task right now, but will get back to this in a day or two. Regards, John From: ntb-clips@yahoogroups.com
      Message 2 of 10 , Aug 27, 2011
      • 0 Attachment
        Thank you, Flo and Art! I'm on another task right now, but will get back to this in a day or two.

        Regards,
        John

        From: ntb-clips@yahoogroups.com [mailto:ntb-clips@yahoogroups.com] On Behalf Of flo.gehrke
        Sent: Saturday, August 27, 2011 13:56
        To: ntb-clips@yahoogroups.com
        Subject: Re: [Clip] Merging two files


        --- In ntb-clips@yahoogroups.com <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.

        John,

        If the two files should be combined 'line by line' like...

        file1 line #1 + file2 line #1
        file1 line #2 + file2 line #2
        etc...

        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...

        ^!SetListDelimiter ^%NL%
        ^!SetArray %Left%=^$GetFileText(^$GetDocumentPath$File1.TXT)$
        ^!SetArray %Right%=^$GetFileText(^$GetDocumentPath$File2.TXT)$
        ^!Set %i%=0

        :Loop
        ^!Inc %i%
        ^!If ^%i% > ^%Left0% Out
        ^!AppendtoFile ^$GetDocumentPath$NewFile.TXT ^%Left^%i%% ^%Right^%i%%^%NL%
        ^!Goto Loop

        :Out
        ; In case you want to replace the original File1...
        ^!RecycleFile ^$GetDocumentPath$File1.txt
        ^!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...

        ^!SetListDelimiter ^%NL%
        ^!SetArray %Left%=^$GetFileText(^$GetDocumentPath$File1.TXT)$
        ^!SetArray %Right%=^$GetFileText(^$GetDocumentPath$File2.TXT)$
        ; Strip empty records from end of array (by courtesy of Eb)
        ^!If "^%Left^%Left0%%" <> "^%Empty%" Skip Else ^!Dec %Left0%
        ^!Goto Skip_-1
        ^!Set %i%=0

        :Loop
        ^!Inc %i%
        ^!If ^%i% > ^%Left0% End
        ^!InsertText ^%Left^%i%% ^%Right^%i%%^%NL%
        ^!Goto Loop

        If the result is correct save it as File1.TXT replacing the original file.

        Regards,
        Flo



        [Non-text portions of this message have been removed]
      • Ian NTnerd
        John, 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
        Message 3 of 10 , Aug 29, 2011
        • 0 Attachment
          John,

          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.
          ^!SetListDelimiter ^P
          ^!SetArray %FileA%=^$GetFileText("^%oldFile1%")$
          ^!SetArray %FileB%=^$GetFileText("^%oldFile2%")$
          ; Use the "loop element" from clip assistant
          ^!Set %Index%=0
          ^!Set %LoopCount%=^%FileA0%
          ^!IfCancel End
          :LoopStart
          ^!Inc %Index%
          ^!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 " "
          ^!setarray %linedata%=^%File2^%index%%
          ^!AppendtoFile "^%newFile%" ^%File1^%index%% ^%linedata2%^%nl%

          ^!GoTo LoopStart
          :EndLoop

          Ian

          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.
          > (file1)
          >
          > 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.
          >
          > Thanks!
          > John
          >
          >
          >
          >
          > ------------------------------------
          >
          > Fookes Software: http://www.fookes.com/
          > NoteTab website: http://www.notetab.com/
          > NoteTab Discussion Lists: http://www.notetab.com/groups.php
          >
          > ***
          > Yahoo! Groups Links
          >
          >
          >
          >
        Your message has been successfully submitted and would be delivered to recipients shortly.