find and delete lines containing one of the strings in an array

find and delete lines containing one of the strings in an array

1
NewbieNewbie
1

    Oct 26, 2015#1

    New to the forum; very new to UltraEdit but I do have some background in OOP. I have a .txt file that I need to edit. I have an array of strings (about 100) and I want to delete the entire line that contains any of those strings. I am using version 18.10.0.1010. Any help would be appreciated.

    6,687586
    Grand MasterGrand Master
    6,687586

      Oct 26, 2015#2

      This task is similar to Delete fast all lines containing string.

      Here is an UltraEdit script for this task.
      • Copy & paste script code into a new file.
      • Copy your strings into the array asStrings at top of the script.
      • Then save the script as ASCII file, for example with file name DeleteAllLinesFromStringArray.js.
      • Add the script with Scripting - Scripts to list of scripts.
      • Open the file with the lines to remove or make it the active file.
      • Run the script via click on it in menu Scripting.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         var asStrings = [ "word1", "string2", "one more string", "etc\\." ];
      
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         // Make sure the last line of file has also a line termination.
         UltraEdit.activeDocument.bottom();
         if (UltraEdit.activeDocument.isColNumGt(1))
         {
            UltraEdit.activeDocument.insertLine();
            // If last line has indenting whitespaces and auto-indent feature
            // is enabled, UltraEdit inserts not only the line termination,
            // but also the indenting spaces/tabs which must be removed to
            // let the file really end with a line termination.
            if (UltraEdit.activeDocument.isColNumGt(1))
            {
               UltraEdit.activeDocument.deleteToStartOfLine();
            }
         }
      
         // Define the parameters for Perl regular expression Replace All deleting
         // all lines containing one of the strings in the array defined above.
         UltraEdit.perlReOn();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=false;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.searchInColumn=false;
         UltraEdit.activeDocument.findReplace.preserveCase=false;
         UltraEdit.activeDocument.findReplace.replaceAll=true;
         UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
      
         // Run the following loop until all strings from array at top of this
         // script was used in the Perl regular expression Replace All executed
         // inside the loop to delete the lines containing one of the strings.
         var nNextStringIndex = 0;
         while (nNextStringIndex < asStrings.length)
         {
            // A Perl OR expression is used to delete lines containing one of the
            // strings defined at top of this script. But as number of strings in
            // an OR expression is not unlimited (depends on length of the strings),
            // the number for OR expression is set to a maximum of 50 per loop run.
            var nStringsForThisReplace = asStrings.length - nNextStringIndex;
            if (nStringsForThisReplace > 50) nStringsForThisReplace = 50;
            var nLastStringIndex = nNextStringIndex + nStringsForThisReplace;
      
            // Extract (copy) the first 50 strings or all remaining from array
            // of strings into a new array which is joined to a single string
            // with | which is the OR operator in a Perl regular expression.
            var sPerlRegExOR = asStrings.slice(nNextStringIndex,nLastStringIndex).join("|");
      
            // Build the entire Perl regular expression search string which finds
            // entire lines containing one of the strings in the OR expression.
            // All 3 types of line terminations (DOS/UNIX/MAC) are supported.
            var sPerlRegExSearch = "^.*(?:" + sPerlRegExOR + ").*(?:\\r?\\n|\\r)";
      
            // Move caret to top of file. It should be enough to do that once
            // outside the loop, but for safety it is done inside the loop.
            UltraEdit.activeDocument.top();
      
            // Run the Perl regular expression Replace all deleting all lines
            // completely containing one of the strings from array at top.
            UltraEdit.activeDocument.findReplace.replace(sPerlRegExSearch,"");
      
            // Set the index in string array to next string to process.
            nNextStringIndex = nLastStringIndex;
         }
      }
      
      The script mainly runs a Perl regular expression Replace All with the search string ^.*(?:word1|string2|one more string|etc\.).*(?:\r?\n|\r) to match entirely a line containing one of the strings in first OR expression in non-capturing group.

      If the about 100 strings are short, it is most likely possible to run the Perl regular expression Replace All also without using a script.

      Note: If one of the strings contains a Perl regular expression character like . or + or *, the character must be escaped with a backslash in Replace dialog, see etc\. in search string, and with 2 backslash in string in the script, see etc\\. in script code.
      Best regards from an UC/UE/UES for Windows user from Austria