This script performs a regular expression (regexp) search of the active document, writing unique matches to the Windows clipboard.
the result would be one line on the clipboard:
Although there are more than a few occurrences of "index" in this script, duplicates are removed before the list is written to the clipboard. By default the sort is case-insensitive; ergo a single line is left when (case-insensitive) duplicates are removed.
Ok, so how is this script useful?
I use UE extensively to write PowerShell scripts. I often create XML configuration files for automated product installation. These installations usually required a number of Windows domain user accounts which must be embedded in these XML files. In addition, these accounts are sometimes coded in more than one place.
As part of installation setup I must create each user account. To this end I use a list of user accounts which I run against a batch file. I use this script to create the account list by running the script against the
populated XML configuration file using this regexp:
The result is a list of unique user account names as I use them in my account creation process
you get a list of formally defined variables:
Here is the complete script:
- This script was written and tested using UEStudio version 10.20.0.1001 running on Windows Server 2008 R2 Enterprise.
RegExp is enabled before the search is executed, activating your default regexp engine (I use the UE engine). You can force the script to use the UE, Perl, or Unix engine by uncommenting/commenting a couple of lines near the search execution command.
Each regexp match is placed on the Windows clipboard sorted (ascending, case-insensitive) and with duplicate matches removed. The duplicate removal can be changed by commenting/uncommenting lines in this script's sortDoc() function. You can also modify any of the sorting option lines to suit your needs.
Code: Select all
index
Code: Select all
index
Ok, so how is this script useful?
I use UE extensively to write PowerShell scripts. I often create XML configuration files for automated product installation. These installations usually required a number of Windows domain user accounts which must be embedded in these XML files. In addition, these accounts are sometimes coded in more than one place.
As part of installation setup I must create each user account. To this end I use a list of user accounts which I run against a batch file. I use this script to create the account list by running the script against the
populated XML configuration file using this regexp:
Code: Select all
domainName\\[a-zA-Z][a-zA-Z0-9]++
- Note: This particular regexp may not work for you as Windows allows characters not specified here and there is a length limit to account names...I just don't need those details for my environment.
Code: Select all
var [a-zA-Z][a-zA-Z0-9\\-_]++
Code: Select all
var callerDocIdx
var callerIdx
var frMatchCase
var frMatchWord
var frMode
var frRegExp
var frSearchAscii
var frSearchDown
var frSearchInColumn
var homeDocIdx
var i
var outputDocIdx
var searchUserName
var tabindex
Code: Select all
////////////////////////////////////////////////////////
/// extractRegExpReults.js
///
/// Searches a document and saves results to the
/// Windows clipboard.
///
/// o The search is a regular expression. RegExp is enabled before the search is executed, so it
/// uses the default UE RegExp engine. You can use the Perl or Unex engine by uncommenting
/// lines in this script (near the search execution).
///
/// o Each RegExp match is placed on the Windows clipboard: Sorted (ascending) and duplicates
/// removed. The duplicate removal can be changed by uncommenting line(s) in the sortDoc()
/// function below. You can also modify any of the sorting option lines to suit your needs.
///
/////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////
// main function - called by last line in this file
////////////////////////////////////////////////////
function main() {
var searchUserName = UltraEdit.getString("Enter a search expression.",1); // Get the search expression.
var homeDocIdx = getActiveDocumentIndex(); // Remember the target document index
var outputDocIdx = UltraEdit.document.length; // Remember temp document index
UltraEdit.newFile(); // Create a temp document (becomes the active document)
UltraEdit.document[homeDocIdx].setActive(); // Reset source document to active after newFile()
UltraEdit.activeDocument.top(); // Start the search at the beginning of the document
// Save current UE search settings
var frMode = UltraEdit.activeDocument.findReplace.mode;
var frMatchCase = UltraEdit.activeDocument.findReplace.matchCase;
var frMatchWord = UltraEdit.activeDocument.findReplace.matchWord;
var frRegExp = UltraEdit.activeDocument.findReplace.regExp;
var frSearchAscii = UltraEdit.activeDocument.findReplace.searchAscii;
var frSearchDown = UltraEdit.activeDocument.findReplace.searchDown;
var frSearchInColumn = UltraEdit.activeDocument.findReplace.searchInColumn;
UltraEdit.activeDocument.findReplace.mode=0; // Set search options
UltraEdit.activeDocument.findReplace.matchCase=false;
UltraEdit.activeDocument.findReplace.matchWord=false;
UltraEdit.activeDocument.findReplace.regExp=true;
UltraEdit.activeDocument.findReplace.searchAscii=false;
UltraEdit.activeDocument.findReplace.searchDown=true;
UltraEdit.activeDocument.findReplace.searchInColumn=false;
// Uncomment the appropriate line for the regExp engine you want to use:
//UltraEdit.perlReOn(); // Perl
UltraEdit.ueReOn(); // UltraEdit
//UltraEdit.unixReOn(); // Unix
// Find all regExp matches and write them to the temporary document
while(UltraEdit.activeDocument.findReplace.find(searchUserName)) {
UltraEdit.document[outputDocIdx].write(UltraEdit.activeDocument.selection + "\r\n");
}
sortDoc(outputDocIdx); // Sort the results in the temporary document
copyDocToClipboard(outputDocIdx, 0); // Copy to clipboard - Using Widows clipboard.
UltraEdit.closeFile(UltraEdit.document[outputDocIdx].path,2); // Dispose of the temporary document
// Restore the original settings for UE search
UltraEdit.activeDocument.findReplace.mode = frMode;
UltraEdit.activeDocument.findReplace.matchCase = frMatchCase;
UltraEdit.activeDocument.findReplace.matchWord = frMatchWord;
UltraEdit.activeDocument.findReplace.regExp = frRegExp;
UltraEdit.activeDocument.findReplace.searchAscii = frSearchAscii;
UltraEdit.activeDocument.findReplace.searchDown = frSearchDown;
UltraEdit.activeDocument.findReplace.searchInColumn = frSearchInColumn;
UltraEdit.activeDocument.top();
return // Ends main() - effectively exits the script.
}
// ////////////////////////////////////////////////////////
// // sub functions
// ////////////////////////////////////////////////////////
// //////////////////////////////////////
// sortDoc()
// //////////////////////////////////////
// Sorts specified document by line and ascending;
// removes duplicate lines. Target document is
// referenced by index number.
function sortDoc(targetIdx) {
var callerIdx = getActiveDocumentIndex(); // Remember the caller's active document
UltraEdit.document[targetIdx].setActive(); // Set the sort target
UltraEdit.activeDocument.sort.ascending=true; // Set sort options
UltraEdit.activeDocument.sort.col1Start=1;
UltraEdit.activeDocument.sort.col1End=-1;
UltraEdit.activeDocument.sort.col2Start=0;
UltraEdit.activeDocument.sort.col2End=0;
UltraEdit.activeDocument.sort.col3Start=0;
UltraEdit.activeDocument.sort.col3End=0;
UltraEdit.activeDocument.sort.col4Start=0;
UltraEdit.activeDocument.sort.col4End=0;
UltraEdit.activeDocument.sort.ignoreCase=true;
UltraEdit.activeDocument.sort.removeDuplicates=2;
UltraEdit.activeDocument.sort.remKey1=true;
UltraEdit.activeDocument.sort.remKey2=true;
UltraEdit.activeDocument.sort.remKey3=true;
UltraEdit.activeDocument.sort.remKey4=true;
UltraEdit.activeDocument.sort.type=0;
UltraEdit.activeDocument.sort.sort(); // Sort the target document
UltraEdit.document[callerIdx].setActive(); // Restore the caller's active document
return // done.
}
// //////////////////////////////////
// copyDocToClipboard()
// //////////////////////////////////
// Copy entire document (specified by index number)
// to clipboard (also specified by index number)
function copyDocToClipboard(documentIdx, clipIdx) {
var callerDocIdx = getActiveDocumentIndex(); // Remember the current active document
try {
UltraEdit.document[documentIdx].setActive() // Activate copy source document
}
catch (e) {
throw "copyDocToClipboard: Invalid document index.";
}
if (clipIdx < 0) { throw "Clipboard index out of range."; } // Check clipboard index range.
if (clipIdx > 9) { throw "Clipboard index out of range."; }
UltraEdit.selectClipboard(clipIdx); // Select and clear the active clipboard
UltraEdit.clearClipboard();
UltraEdit.activeDocument.selectAll(); // Select the text, copy to active clipboard
UltraEdit.clipboardContent = UltraEdit.activeDocument.selection;
UltraEdit.document[callerDocIdx].setActive(); // Restore the caller's active document
return // done.
}
// Get the index for the active document.
// I lifted this from the UE user forum. Thank
// you jorrasdk,
function getActiveDocumentIndex() {
var tabindex = -1; /* start value */
for (var i = 0; i < UltraEdit.document.length; i++)
{
if (UltraEdit.activeDocument.path==UltraEdit.document[i].path) {
tabindex = i;
break;
}
}
return tabindex;
}
main()