Write RegExp Match Lines to a New File

Write RegExp Match Lines to a New File

2
NewbieNewbie
2

    Aug 17, 2011#1

    This brief script searches for regular expression matches. The entire line where each match is found is copied to a newly created file.
    • 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.
    If you searched this script for the term

    Code: Select all

    var
    the result would be :

    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++)
    
    Here is the entire script:

    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()

    6,686585
    Grand MasterGrand Master
    6,686585

      Aug 18, 2011#2

      Nice script. I have some suggestions for further improvement.
      1. Instead of setting the caret to top of active file, it should remember the current caret position and restore it.
      2. The new file should be active while searching for the strings because this is faster when the document windows are maximized because of no display updates necessary in the file active on script start.
      3. The found lines should not be written directly to the new file, but collected in a string variable and written at once to new file after finishing the search. This is faster because just 1 undo step to record for UltraEdit for the new file. The disadvantage of this approach is that if the script is executed on a very large file and finds millions of lines, it could fail because of an out of memory issue.
      4. The new file should be closed without saving and an error message should be displayed if the script does not find any line matching the regular expression.
      Your script with the additions and changes according to my suggestions:

      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.
      ///
      /////////////////////////////////////////////////////////////////////////////////////////////////
      
      ////////////////////////////////////////////////////
      // main function - called by last line in this file
      ////////////////////////////////////////////////////
      function main() {
      
          // Remember current position of caret in the active file.
          var nLineNum = UltraEdit.activeDocument.currentLineNum;
          var nColNum  = UltraEdit.activeDocument.currentColumnNum;
          if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nColNum++;
      
          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].top();                           // Start the search at the beginning of the document
      
          // Save current UE search settings
          var frMode              = UltraEdit.document[homeDocIdx].findReplace.mode;
          var frMatchCase         = UltraEdit.document[homeDocIdx].findReplace.matchCase;
          var frMatchWord         = UltraEdit.document[homeDocIdx].findReplace.matchWord;
          var frRegExp            = UltraEdit.document[homeDocIdx].findReplace.regExp;
          var frSearchAscii       = UltraEdit.document[homeDocIdx].findReplace.searchAscii;
          var frSearchDown        = UltraEdit.document[homeDocIdx].findReplace.searchDown;
          var frSearchInColumn    = UltraEdit.document[homeDocIdx].findReplace.searchInColumn;
      
          UltraEdit.document[homeDocIdx].findReplace.mode=0;              // Set search options
          UltraEdit.document[homeDocIdx].findReplace.matchCase=false;
          UltraEdit.document[homeDocIdx].findReplace.matchWord=false;
          UltraEdit.document[homeDocIdx].findReplace.regExp=true;
          UltraEdit.document[homeDocIdx].findReplace.searchAscii=false;
          UltraEdit.document[homeDocIdx].findReplace.searchDown=true;
          UltraEdit.document[homeDocIdx].findReplace.searchInColumn=false;
      
          // Uncomment the appropriate line for the regExp engine you want to use:
          //UltraEdit.perlReOn();   // Perl
          UltraEdit.ueReOn();       // UltraEdit
          //UltraEdit.unixReOn();   // Unix
      
          var sFoundLines = "";
          // Find all regExp matches and collect them first in a string variable.
          while(UltraEdit.document[homeDocIdx].findReplace.find(searchUserName)) {
      
              UltraEdit.document[homeDocIdx].selectLine();
              sFoundLines += UltraEdit.document[homeDocIdx].selection;
          }
      
          // Restore the original settings for UE search
          UltraEdit.document[homeDocIdx].findReplace.mode           = frMode;
          UltraEdit.document[homeDocIdx].findReplace.matchCase      = frMatchCase;
          UltraEdit.document[homeDocIdx].findReplace.matchWord      = frMatchWord;
          UltraEdit.document[homeDocIdx].findReplace.regExp         = frRegExp;
          UltraEdit.document[homeDocIdx].findReplace.searchAscii    = frSearchAscii;
          UltraEdit.document[homeDocIdx].findReplace.searchDown     = frSearchDown;
          UltraEdit.document[homeDocIdx].findReplace.searchInColumn = frSearchInColumn;
      
          // Restore the caret position in the active file on script start.
          UltraEdit.document[homeDocIdx].gotoLine(nLineNum,nColNum);
      
          if (sFoundLines.length) {
              UltraEdit.activeDocument.write(sFoundLines);
              UltraEdit.activeDocument.top();
          }
          else {
              UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
              if (UltraEdit.messageBox) {
                  UltraEdit.messageBox("No line with a matching string found.","extractRegExpMatchedLines");
              }
          }
          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()
      Extra hint: It is not necessary to remember current find/replace option values on script start and restore them on script end. The changes on the find/replace options within a macro or script are always active only during the execution of the macro / script. The find/replace options as displayed in the Find/Replace dialog are not affected by the finds/replaces executed by a macro or script. The only exception was the regular expression engine in older versions of UltraEdit. But since the regular expression engine can be chosen directly in the Find/Replace dialog, also the regular expression engine selection in the macro/script is just temporary for the execution of the macro/script. Remembering and restoring find/replace options makes only sense for a script function included and called perhaps in another script.