Copy lines in the active document containing strings listed in another file

Copy lines in the active document containing strings listed in another file

4
NewbieNewbie
4

    Apr 22, 2009#1

    Hi, great script. I want to copy the found lines instead of deleting.

    Code: Select all

    while(UltraEdit.activeDocument.findReplace.find("^c")) {
                 UltraEdit.activeDocument.deleteLine();
    I thought well just replace

    Code: Select all

    UltraEdit.activeDocument.deleteLine()
    with

    Code: Select all

    UltraEdit.activeDocument.copyAppend();
    but that does not work :(. Don't shoot me. I am new in UE scripting. What does the ^c mean?

    6,602547
    Grand MasterGrand Master
    6,602547

      Apr 22, 2009#2

      Here is the script to copy all lines containing a string from a list of strings in a second file into the Windows clipboard. There were originally only 6 lines inserted and 1 deleted in comparison to the referenced script which deletes the lines with the found string. In the meantime additional lines were inserted for impoving speed, see my next post.

      Special meaning of ^c is documented on help page of the Find command and means simply content of active clipboard, clipboard 9 for this script for the searches.

      Code: Select all

      // Document 0 (most left one) must be the active document.
      // Document 1 (second file right to active file) must be the file with the strings.
      if ((UltraEdit.document.length >= 2) && (UltraEdit.activeDocument.path == UltraEdit.document[0].path)) {
      
         // Always specify the search engine which should be used.
         UltraEdit.ueReOn();
         UltraEdit.selectClipboard(0);
         UltraEdit.clearClipboard();
         // Use a clipboard instead of a string variable in
         // case of file with the strings is a Unicode file.
         UltraEdit.selectClipboard(9);
         // Open a new file which automatically gets the active one. If the document
         // windows are maximized this new file avoids display updates on the file
         // to search for the strings which makes execution of the script faster.
         UltraEdit.newFile();
      
         /* Verify if the last line of the file with the strings has a line ending.
            If this is not the case insert one and make sure that the auto indent
            feature has not added additional preceding white-spaces. The last line
            of the file with the strings must have a line ending or the script
            would run into an endless loop because cursor does never reach the
            end of the file with the strings. */
         UltraEdit.document[1].bottom();
         if (UltraEdit.document[1].isColNumGt(1)) {
            UltraEdit.document[1].insertLine();
            if (UltraEdit.document[1].isColNumGt(1)) {
               UltraEdit.document[1].deleteToStartOfLine();
            }
         }
         UltraEdit.document[1].top();
         // For security same check also on the active file.
         UltraEdit.document[0].bottom();
         if (UltraEdit.document[0].isColNumGt(1)) {
            UltraEdit.document[0].insertLine();
            if (UltraEdit.document[0].isColNumGt(1)) {
               UltraEdit.document[0].deleteToStartOfLine();
            }
         }
         // Specify once all the find options.
         UltraEdit.document[0].findReplace.searchDown=true;
         UltraEdit.document[0].findReplace.mode=0;
         UltraEdit.document[0].findReplace.matchCase=false;
         UltraEdit.document[0].findReplace.matchWord=false;
         UltraEdit.document[0].findReplace.regExp=false;
      
         // Open the output window when not already visible.
      //   if (UltraEdit.outputWindow.visible == false) UltraEdit.outputWindow.showWindow(true);
      
         // Loop through the lines of the file with the strings.
         while(!UltraEdit.document[1].isEof()) {
      
            UltraEdit.document[1].startSelect();
            UltraEdit.document[1].key("END");
            if (UltraEdit.document[1].isSel()) {
               UltraEdit.document[1].copy();
               UltraEdit.document[1].endSelect();
               UltraEdit.document[1].key("HOME");
               UltraEdit.document[1].key("DOWN ARROW");
            }
            else {
               // Ignore empty lines!
               UltraEdit.document[1].endSelect();
               UltraEdit.document[1].key("HOME");
               UltraEdit.document[1].key("DOWN ARROW");
               continue;
            }
      
      //      UltraEdit.outputWindow.write("Searching for \""+UltraEdit.clipboardContent+"\"");
      
            // Start at the beginning of the file.
            UltraEdit.document[0].top();
            while(UltraEdit.document[0].findReplace.find("^c")) {
               UltraEdit.selectClipboard(0);
               UltraEdit.document[0].selectLine();
               UltraEdit.document[0].copyAppend();
               UltraEdit.selectClipboard(9);
            }
         }
         UltraEdit.document[0].top();
         UltraEdit.document[1].top();
         UltraEdit.clearClipboard();
         UltraEdit.selectClipboard(0);
         // Remove the comments on the 3 lines below to save the found
         // lines into the file specified in the saveAs command.
      //   UltraEdit.activeDocument.paste();
      //   UltraEdit.saveAs("Full Path\\Filename.ext");
      //   UltraEdit.clearClipboard();
         UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
         UltraEdit.document[0].setActive();
      }

      4
      NewbieNewbie
      4

        Apr 22, 2009#3

        Thank you very much.

        1
        NewbieNewbie
        1

          Dec 02, 2009#4

          Very good example. Is it possible to store the output into a new file and place it on the desktop?

          Hi I have run this script with 130 parameters with a file that has 1 000 000 lines. It keeps on executing doesn't finish, is there a limit to how big the file is or how many parameters you can search by?

          cheers

          6,602547
          Grand MasterGrand Master
          6,602547

            Dec 03, 2009#5

            I have modified the script above to make it faster by opening a new file which is the active one during script execution. If the document windows are opened maximized the display updates are avoided with this little trick. I have also added the lines at bottom of the script (in comments) for saving the collected data.

            There are no limits except your memory. The lines are collected in the Windows clipboard. But I have never tested it with large or huge files. If you want process update information during script execution I suggest to write into the output window for which string the script currently searches. I have added the 2 lines needed for this process info as comments.
            Best regards from an UC/UE/UES for Windows user from Austria

            4
            NewbieNewbie
            4

              Apr 09, 2010#6

              I just thought I'd share this:

              I used the above script to search for lines in a file (first open file in UE) that contained my subject value (a single word in the second open file in UE).

              My txt file was 88 million lines long, yes, 88 million lines! (Actually, 88,030,997 lines)

              This script accurately returned 1530 lines that matched, and saved them in to the txt file I had supplied by un-commenting the three lines at the end of the script.

              This script accomplishes this task in 8 hrs and 45 minutes.
              Thanks for your efforts!

              J.

              17
              Basic UserBasic User
              17

                Dec 17, 2012#7

                Great!

                I'm quite new to js stuff (I'm familiar with C++ though). I just thought of this kind of functionality today. Now it's already here fully functional.

                I've just tested it and found it worked just exactly as what I expected.

                Thank you very much.