This brief script searches for regular expression matches. The entire line where each match is found is copied to a newly created file.
the result would be :
Here is the entire 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 line is written to a newly created file.
Code: Select all
var
Code: Select all
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
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;
var tabindex = -1; /* start value */
for (var i = 0; i < UltraEdit.document.length; i++)
Code: Select all
////////////////////////////////////////////////////////
/// extractRegExpMatchedLines.js
///
/// Searches a document and saves result line(s) to a new file.
///
/// 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 Unix engine by uncommenting
/// lines in this script (near the search execution).
///
/// o Each RegExp match is wimply written to a new file, unsorted. The entire line, including
/// any newline character(s) is written. A new file is always created (even if there are no
/// matches).
///
/////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////
// 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.activeDocument.selectLine();
UltraEdit.document[outputDocIdx].write(UltraEdit.activeDocument.selection);
}
// 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
// ////////////////////////////////////////////////////////
// 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()