Tapatalk

How to use JavaScript code in UltraEdit to extract all lines with AUTHORIZATION SKIPPED to a new file?

How to use JavaScript code in UltraEdit to extract all lines with AUTHORIZATION SKIPPED to a new file?

2
NewbieNewbie
2

    Sep 17, 2023#1

    Hi all.

    I'm using UltraEdit version 25.20.0.166. I would like to extract special text from the lines of a file. For this purpose I tried to use JavaScript code by following these steps:
    1. I created a JavaScript file - extractText.js.
    2. Then launched UltraEdit and in menu selected the Advanced.
    3. Selected All Scripts option.
    4. Selected extractText.js file.
    5. Clicked Play script.
    But, unfortunately, the text was not extracted.

    Can I use JavaScript code in UltraEdit version 25.20.0.166? And if so, what did I do wrong?

    Thanks

    6,685587
    Grand MasterGrand Master
    6,685587

      Sep 18, 2023#2

      The questions cannot be answered without seeing the contents of the file extractText.js.

      I suggest looking on Find strings with a regular expression and output them to new file.
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Sep 18, 2023#3

        Hi Mofi. Thanks for the reply.

        That is the code of the script file.

        Code: Select all

        var searchText = "AUTHORIZATION SKIPPED"; // Text to search for
        var extractedLines = [];
        
        for (var i = 0; i < UltraEdit.document.length; i++) {
            var line = UltraEdit.document[i];
        
            // Check if the line contains the specified text
            if (line.indexOf(searchText) !== -1) {
                extractedLines.push(line);
            }
        }
        
        // Create a new document and paste the extracted lines
        var newDoc = UltraEdit.createDocument();
        newDoc.selectAll();
        newDoc.copy();
        newDoc.close();
        UltraEdit.activeDocument.paste();
        Thanks.

        6,685587
        Grand MasterGrand Master
        6,685587

          Sep 18, 2023#4

          Your script produces in the Output Window the error output:

          Code: Select all

          An error occurred on line 14:
                var newDoc = UltraEdit.createDocument();
          Script failed.
          The UltraEdit object has no member function createDocument. The correct function for creating a new file/document would be UltraEdit.newFile() which does not return something.

          The Output Window should be permanently visible on execution of a script in development on which not all lines have been successfully tested to see errors detected by the JavaScript interpreter on parsing the script and during script execution.

          Selecting everything in the new file, which is of course nothing as it is an empty file, copying nothing to clipboard, closing the new file and pasting nothing into active file would no make much sense too.

          UltraEdit.document is not an array of lines of active document, but an array of opened files (documents). UltraEdit.document.length is the number of opened files and not the number of lines in active document. There is no property with number of lines of active file because of very large files can be opened in UltraEdit also without counting lines at all which can take a lot of time.

          I recommend opening the help of UltraEdit on using UltraEdit on Windows, switch to tab Index, type in the edit field script and double click in the list below on Scripting commands explaining (nearly) all UltraEdit specific scripting commands (objects, functions, properties).

          There can be opened also the Tag List view from ribbon tab Layout by checking Tags in group Lists and selecting the tag list group UE/UES Script Commands. UltraEdit specific functions and properties can be inserted from this list with a double click on a list item. The caret is placed automatically where to insert something first like first function parameter or name of a variable getting assigned the return value of a function.

          There can be downloaded the ZIP file FindStringsToNewFile.zip and extracted into a directory of your choice like %APPDATA%\IDMComp\UltraEdit\Scripts which must be created first. See also: Where should UltraEdit/UEStudio scripts be stored and where to enter the description?

          Next click on ribbon tab Advanced on command All scripts in the group Script and add the script FindStringsToNewFile.js to the list of scripts with a description of your choice entered in the File Open dialog opened for selecting the script file.

          The script is ready for usage now for running it on a file containing lines of interest. Try it out by opening the data file and clicking on ribbon tab Layout on command Play script and next on FindStringsToNewFile.js if there is more than one script in the list. A script execution from the list can be done also by opening Script list view with checking on ribbon tab Layout the item Scripts.

          The scripts prompts you for the string to search. There must be entered .*AUTHORIZATION SKIPPED.* for getting all lines containing case-insensitive the string AUTHORIZATION SKIPPED written completely into a new file.

          The script contains

          Code: Select all

          var sSearchString = "";
          which can be modified to

          Code: Select all

          var sSearchString = ".*AUTHORIZATION SKIPPED.*";
          to always use that Perl regular expression search string instead of prompting the user for a search expression.

          There could be used also the following fully commented script which makes a copy of the active file and deletes in the new file all lines not containing the string AUTHORIZATION SKIPPED.

          Code: Select all

          if (UltraEdit.document.length > 0)  // Is any file opened?
          {
             // Define environment for this script.
             UltraEdit.insertMode();
             // UltraEdit.columnModeOff() is for UltraEdit for Window while
             // UltraEdit.activeDocument.columnModeOff() is for UltraEdit for Linux/Mac.
             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
          
             // Select everything in active file.
             UltraEdit.activeDocument.selectAll();
          
             // Is the active file not an empty file?
             if (UltraEdit.activeDocument.isSel())
             {
                // Select user clipboard 9 to prevent overwriting the clipboard of
                // the operating system with the temporary data of active file.
                UltraEdit.selectClipboard(9);
                // Copy everything in active file to user clipboard 9.
                UltraEdit.activeDocument.copy();
                // Move caret to top of the active file just to discard the selection.
                UltraEdit.activeDocument.top();
          
                // Create a new file.
                UltraEdit.newFile();
                // Paste the copied data into this new file with assuming the new file
                // uses the same character encoding as the active file on beginning of
                // the script execution. A different line ending type is automatically
                // considered by UltraEdit on paste on using the default configuration.
                UltraEdit.activeDocument.paste();
                // Clear user clipboard 9 to free memory no longer needed.
                UltraEdit.clearClipboard();
                // Make the clipboard of the operating system the active clipboard.
                UltraEdit.selectClipboard(0);
          
                // Has the last line in the new file no line ending?
                if (UltraEdit.activeDocument.isColNumGt(1))
                {
                   // Append a line ending at the end of the new file.
                   UltraEdit.activeDocument.insertLine();
                   // UltraEdit may automatically insert also indenting spaces/tabs in
                   // case of the auto-indent feature is enabled and the last line in
                   // the file has indenting spaces/tabs. In this case delete the
                   // automatically appended indenting spaces/tabs.
                   if (UltraEdit.activeDocument.isColNumGt(1))
                   {
                      UltraEdit.activeDocument.deleteToStartOfLine();
                   }
                }
          
                // Move the caret from bottom to top of new file.
                UltraEdit.activeDocument.top();
          
                // Select the Perl regular expression engine as search engine.
                UltraEdit.perlReOn();
          
                // Define all parameters for a case-insensitive Perl regular expression
                // replace all executed from top of the new file which deletes all
                // lines not containing the string "AUTHORIZATION SKIPPED" in new file.
                UltraEdit.activeDocument.findReplace.mode=0;
                UltraEdit.activeDocument.findReplace.matchCase=false;
                UltraEdit.activeDocument.findReplace.matchWord=false;
                UltraEdit.activeDocument.findReplace.regExp=true;
                UltraEdit.activeDocument.findReplace.searchDown=true;
                // The IF condition is for UltraEdit version 13.xx which does not have
                // the search in column feature. Just the line inside the IF condition
                // is needed on newer versions of UltraEdit.
                if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
                {
                   UltraEdit.activeDocument.findReplace.searchInColumn=false;
                }
                UltraEdit.activeDocument.findReplace.preserveCase=false;
                UltraEdit.activeDocument.findReplace.replaceAll=true;
                UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
          
                // Run the replace for deletion of all lines not containing the string
                // of interest. Each backslash must be escaped with one more backslash
                // in a JavaScript string to pass the correct search expression to the
                // Perl regular expression engine of UltraEdit.
                UltraEdit.activeDocument.findReplace.replace("^(?:(?!AUTHORIZATION SKIPPED).)*$(?:\\r?\\n|\\r)?","");
             }
          }
          
          The forum topic How to delete all lines NOT containing specific word or string or expression? explains the Perl regular expression used for deleting all lines not containing AUTHORIZATION SKIPPED.
          Best regards from an UC/UE/UES for Windows user from Austria