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

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

2
NewbieNewbie
2

    Apr 08, 2009#1

    Hi,

    I'm using this script:

    Code: Select all

    // Start at the beginning of the file
    UltraEdit.activeDocument.top();
    
    // Search string variable used for deletion of lines
    var delString = UltraEdit.getString("Delete lines containing what?",1);
    
    // Establish our search string for the loop condition
    UltraEdit.activeDocument.findReplace.find(delString);
    
         // Loop through the file and delete found lines
         while (UltraEdit.activeDocument.isFound()){
         UltraEdit.activeDocument.top();
         UltraEdit.activeDocument.findReplace.find(delString);
         
         // We use an if statement here, because when the above "isFound" is evaluated,
         // the script will use the last Find operation for re-evaluation instead of initiating
         // a new one, causing one extra unintended deletion.
         if (UltraEdit.activeDocument.isFound()) {
              UltraEdit.activeDocument.deleteLine();
         }
    }
    Can anyone help me to modify this one to have an input file instead of a pop up waiting for my input because I have a lots of line of input to delete in the active file.

    6,602547
    Grand MasterGrand Master
    6,602547

      Apr 08, 2009#2

      The first version is based on your script. Read the important comment at top of the script.

      Code: Select all

      // Document 0 (most left one) must be the file to update.
      // Document 1 (second file in file tabs bar) must be the file
      // with the strings which should be removed from the first file.
      // The third file can be this script to be able to execute it with
      // clicking on menu item Run Active Script in menu Scripting.
      
      if (UltraEdit.document.length > 1) {
      
         // Always specify the search engine which should be used.
         UltraEdit.ueReOn();
         // Use a clipboard instead of a string variable in
         // case of file with the strings is a Unicode file.
         UltraEdit.selectClipboard(9);
      
         /* 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 first 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.searchInColumn=false;
         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;
      
         // 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].gotoLine(0,1);
               UltraEdit.document[1].key("DOWN ARROW");
            }
            else {
               // Ignore empty lines!
               UltraEdit.document[1].endSelect();
               UltraEdit.document[1].gotoLine(0,1);
               UltraEdit.document[1].key("DOWN ARROW");
               continue;
            }
      
            // Start at the beginning of the file.
            UltraEdit.document[0].top();
            while(UltraEdit.document[0].findReplace.find("^c")) {
               UltraEdit.document[0].deleteLine();
            }
         }
         UltraEdit.document[0].top();
         UltraEdit.document[1].top();
         UltraEdit.clearClipboard();
         UltraEdit.selectClipboard(0);
      }
      I have also a second version which is much faster because it deletes all the lines using a regular expression replace all in UltraEdit style. But you can use this version only if none of your strings contain an UltraEdit regular expression character because the string in the clipboard is interpreted as regular expression string too. And the active file must have DOS line endings, or you replace ^p in the search string by ^n for UNIX line endings. Well, the first version has the advantage that you can log with a few additional commands into the output window or a new file which lines the script has deleted.

      Code: Select all

      // Document 0 (most left one) must be the file to update.
      // Document 1 (second file in file tabs bar) must be the file
      // with the strings which should be removed from the first file.
      // The third file can be this script to be able to execute it with
      // clicking on menu item Run Active Script in menu Scripting.
      
      if (UltraEdit.document.length > 1) {
      
         // Always specify the search engine which should be used.
         UltraEdit.ueReOn();
         // Use a clipboard instead of a string variable in
         // case of file with the strings is a Unicode file.
         UltraEdit.selectClipboard(9);
      
         /* 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 replace options.
         UltraEdit.document[0].findReplace.searchInColumn=false;
         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=true;
         UltraEdit.document[0].findReplace.preserveCase=false;
         UltraEdit.document[0].findReplace.replaceAll=true;
         // Start at the beginning of the file.
         UltraEdit.document[0].top();
      
         // 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].gotoLine(0,1);
               UltraEdit.document[1].key("DOWN ARROW");
            }
            else {
               // Ignore empty lines!
               UltraEdit.document[1].endSelect();
               UltraEdit.document[1].gotoLine(0,1);
               UltraEdit.document[1].key("DOWN ARROW");
               continue;
            }
            UltraEdit.document[0].findReplace.replace("%*^c*^p","");
         }
         UltraEdit.document[0].top();
         UltraEdit.document[1].top();
         UltraEdit.clearClipboard();
         UltraEdit.selectClipboard(0);
      }
      And I can offer also a third version which makes the entire job completely in memory, is therefore very fast and produces only 1 undo step on updated first file. But it can be used only for files which can be loaded completely into memory. So don't use it for files with more than several 100 MB. I have not tested this script on Unicode files.

      Code: Select all

      // Document 0 (most left one) must be the file to update.
      // Document 1 (second file in file tabs bar) must be the file
      // with the strings which should be removed from the first file.
      // The third file can be this script to be able to execute it with
      // clicking on menu item Run Active Script in menu Scripting.
      if (UltraEdit.document.length > 1) {
      
         var sLineTerm = "";
         // Get type of line terminator if version of UE / UES supports it.
         if (typeof(UltraEdit.document[1].lineTerminator) == "number") {
            if (UltraEdit.document[1].lineTerminator == 1) sLineTerm == "\n";
            else if (UltraEdit.document[1].lineTerminator == 2) sLineTerm == "\r";
            else sLineTerm == "\r\n";
         }
      
         // Select everything in the second file.
         UltraEdit.document[1].selectAll();
      
         // Determine type of line terminator from selected text if version
         // of UltraEdit / UEStudio does not support line terminator property.
         if (sLineTerm == "") {
            if (UltraEdit.document[1].selection.indexOf("\r\n") >= 0) sLineTerm = "\r\n";
            else if (UltraEdit.document[1].selection.indexOf("\n") >= 0) sLineTerm = "\n";
            else if (UltraEdit.document[1].selection.indexOf("\r") >= 0) sLineTerm = "\r";
            else sLineTerm = "\r\n";
         }
      
         // Get the selected lines (= strings) into an array of strings.
         var asStrings = UltraEdit.document[1].selection.split(sLineTerm);
         // Discard the selection. Caret is already at top of second file.
         UltraEdit.document[1].top();
      
         // Do the same also for the first file.
         sLineTerm = "";
         if (typeof(UltraEdit.document[0].lineTerminator) == "number") {
            if (UltraEdit.document[0].lineTerminator == 1) sLineTerm == "\n";
            else if (UltraEdit.document[0].lineTerminator == 2) sLineTerm == "\r";
            else sLineTerm == "\r\n";
         }
         UltraEdit.document[0].selectAll();
         if (sLineTerm == "") {
            if (UltraEdit.document[0].selection.indexOf("\r\n") >= 0) sLineTerm = "\r\n";
            else if (UltraEdit.document[0].selection.indexOf("\n") >= 0) sLineTerm = "\n";
            else if (UltraEdit.document[0].selection.indexOf("\r") >= 0) sLineTerm = "\r";
            else sLineTerm = "\r\n";
         }
         // Get the selected lines into an array of strings.
         var asLines = UltraEdit.document[0].selection.split(sLineTerm);
      
         var bLinesDeleted = false;
         // Remove all lines from the array of lines from first file containing
         // any string in the array of strings from the second file.
         for (var nStringIndex = 0; nStringIndex < asStrings.length; nStringIndex++) {
      
            if (asStrings[nStringIndex] == "") continue; // Ignore empty lines.
      
            for (var nLineIndex = 0; nLineIndex < asLines.length; nLineIndex++) {
      
               // Does this line contain the current string?
               if (asLines[nLineIndex].indexOf(asStrings[nStringIndex]) < 0) continue;
      
               // Yes, then remove this line from the array. To continue with
               // next line the index in the lines array must be decreased by
               // one or the next line would be ignored in string search.
               asLines.splice(nLineIndex,1);
               bLinesDeleted = true;
               nLineIndex--;
            }
         }
      
         // Overwrite still selected content of first file with updated lines list.
         if (bLinesDeleted) UltraEdit.document[0].write(asLines.join(sLineTerm));
         // Set caret in first file back to top of the file.
         UltraEdit.document[0].top();
      }

      2
      NewbieNewbie
      2

        Apr 09, 2009#3

        Hi,
        I got it working. Thanks a lot and more power.

        -fenox