User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Help with writing and running scripts
2 posts Page 1 of 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 Any help would be appreciated.
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.

   // Make sure the last line of file has also a line termination.
   if (UltraEdit.activeDocument.isColNumGt(1))
      // 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))

   // Define the parameters for Perl regular expression Replace All deleting
   // all lines containing one of the strings in the array defined above.

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

      // Run the Perl regular expression Replace all deleting all lines
      // completely containing one of the strings from array at top.

      // 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 Austria
2 posts Page 1 of 1