IDM PowerTips

Search and Delete Lines Found with Scripting

We often hear from customers requesting a way to search for and delete lines from their files. UltraEdit does provide this functionality via its powerful scripting engine. Below, we’ll look at how to write a script that does this.

1. Write the basic script

We can use UltraEdit’s very powerful and flexible scripting engine to write a basic script to search for a string and delete it everywhere it occurs. We’ll need to create a new .js file then load it into UltraEdit’s script list to use it.

Once you’ve created a new .js file, begin by defining a variable which will hold the search string to use to delete lines. You could use something like the following:

// Search string variable used for deletion of lines
var delString = "string to find";

But a hard-coded variable would not be useful when your search string changes. Instead, use the built-in getString function to prompt the user for the string.

// Search string variable used for deletion of lines
var delString = UltraEdit.getString("Delete lines containing what?",1);

The integer “1” is included to prevent UltraEdit from inserting the string into the file once the user enters it.

Now, we need to write a loop that will cycle through our file and find the variable delString’s contents, then delete those lines. Javascript loops are very basic and simple to understand; more information is available here. However, we also need a condition that the script checks each time it cycles through the loop, so the loop knows when to exit. We’ll also add a “top” command to make sure we’re starting at the top of the file.

The following code will work exactly as we want it:

// 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();
     }
}
     

Notice the “if” statement as part of our “while” loop. This is used because (as noted in the comments) when evaluating the “while” loop after the last-matched line was deleted, UltraEdit does not initiate a new internal find to evaluate whether the string still exists in the file, but evaluates the last-called find. In this case, the last-called find was used in the last pass through the loop, which was successful. Therefore this results in UltraEdit cycling through the loop again and deleting the top line of the file which is a correct but unintended outcome.

Go ahead and run the script; you should see all lines in the current file containing the string you insert deleted!

2. Update the script to prompt the user on what was done

This script works well, but we’d like to provide some feedback for the user. We want to let the user know how many lines were deleted; or if none were deleted at all. We can add an integer variable (in scripting the letter “i” is usually used) to the script that is incremented in our “while” loop by 1 each time the loop is executed, then call the variable later in the script to report to the user how many lines were deleted. Following is our modified script (modifications and additions are highlighted):

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

// Variable to count found items
i = 0;

     // 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();
          i++;
     }
}

// Report on what was replaced
if (i == 0){
     UltraEdit.messageBox("No occurrences of \"" + delString + "\" found in this file.\r\n
     No lines were deleted.");
} else {
     UltraEdit.messageBox(i + " lines containing \"" + delString + "\" were found and
     deleted in this file.");
}

Note: Notice that we used double-equal signs in the evaluation of “i”. This is because much like other scripting languages, a single equal sign will actually assign a value to the variable. In this case, a single equal sign would revert “i” back to 0. By using the double-equal sign, we are saying “If ‘i’ is equal to 0″.

Also note the use of the shorthand “i++” which in javascript is the equivalent of “i = i + 1“.

Now the user will know how many lines were deleted from the file.

3. Modify the script to create a new file and report all deleted lines

Perhaps instead of simply deleting the lines, you’d like them extracted to a new file to be used in a report. Again, this is very simple with a few small modifications to our script so far. Perhaps most useful is the “cutAppend” command which will hold and update our cut lines onto the clipboard.

We’ll also still want a summary at the top of our report, so we will modify our script to write the total of “i” to the report file instead of alerting us in an UltraEdit message box.

The modified script is below (modifications and additions are highlighted):

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

// Variable to count found items
i = 0;

// Start with nothing on the clipboard
UltraEdit.clearClipboard();

     // Loop through the file, count 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.selectLine();
          UltraEdit.activeDocument.cutAppend();
          i++;
     }
}

if (i == 0){
     UltraEdit.messageBox("No occurrences of \"" + delString + "\" found in this file.\r\n
     No lines were deleted.");
} else {
     // Create a new file for the report
     UltraEdit.newFile();
     UltraEdit.activeDocument.paste();
     UltraEdit.activeDocument.top();
     UltraEdit.activeDocument.write(i + " lines containing \"" + delString + "\" were found and
     deleted in this file.\r\n\r\n");
}

That’s all there is to it! You can modify the script even further if you’d like to include support for regular expressions, or write conditional statements into the script to meet your specific needs. As with many tasks, taking a few minutes to write a very simple script can save you many, many hours of manual labor in the future.