Create log based on search results

Create log based on search results

3
NewbieNewbie
3

    Mar 30, 2023#1

    Hello, 

    I'm trying to do the following with a script, but I'm having a hard time doing so. I have a COBOL program that I'm looking through to make sure procedures are called correctly, but I'm trying to write a script to automate it.

    Example of code I'm trying to find:

    Code: Select all

              perform j000-main thru j000-exit
               go to j000-exit
    
    Steps I'm currently doing:
    1. Replace GO TO with GO TO PERFORM j000-exit
    2. Set variable equal to string between GO TO -EXIT, so PERFORM j000
    3. Log every line containing variable.
    I would very much appreciate if I could get some assistance. :)
    Thank you

    6,686585
    Grand MasterGrand Master
    6,686585

      Mar 30, 2023#2

      I have not really understood the task. It would have been good to see a block of COBOL code before executing the UltraEdit script and the same block how it should look like after script execution and what should be written into the log (output window) by the script for this block.

      Here is nevertheless already a script:

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();
      
         // Clear the active output window.
         UltraEdit.outputWindow.clear();
      
         // Define the parameters for a case-insensitive UltraEdit regular expression find.
         UltraEdit.ueReOn();
         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;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
         {
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
         }
      
         // Search in a loop for a line containing "go to" any other characters and "-exit".
         while (UltraEdit.activeDocument.findReplace.find("go to[^t ]*-exit"))
         {
            var sGoToText = UltraEdit.activeDocument.selection;
            // Does the found and selected text not contain the word "perform" in any csse?
            if (sGoToText.search(/\bperform\b/i) < 0)
            {
               // Overwrite found and selected text by a text with word perform inserted.
               UltraEdit.activeDocument.write(sGoToText.replace(/^(go to)([\t ]+)(.+)$/,"$1$2perform$2$3"));
            }
            // Write to the ouput window the active line number and the string left to "-exit".
            UltraEdit.outputWindow.write("Line " + UltraEdit.activeDocument.currentLineNum.toString(10) +
                                         ": " + sGoToText.replace(/^go to.*\b([^\t ]+)-exit$/i,"$1"));
         }
         UltraEdit.outputWindow.showStatus=false;
         UltraEdit.outputWindow.showWindow(true);
      }
      
      The test sample used by me:

      Code: Select all

      perform j000-main thru j000-exit
      go to j000-exit
      perform j000-main thru j001-exit
      go to    j001-exit
      perform j000-main thru j002-exit
      go to    PERFORM    j002-exit
      perform j000-main thru j003-exit
      go to PERFORM j003-exit
      perform j000-main thru j004-exit
      go to j004-exit
      
      The result after script execution:

      Code: Select all

      perform j000-main thru j000-exit
      go to perform j000-exit
      perform j000-main thru j001-exit
      go to    perform    j001-exit
      perform j000-main thru j002-exit
      go to    PERFORM    j002-exit
      perform j000-main thru j003-exit
      go to PERFORM j003-exit
      perform j000-main thru j004-exit
      go to perform j004-exit
      The lines shown in opened output window:

      Code: Select all

      Line 2: j000
      Line 4: j001
      Line 6: j002
      Line 8: j003
      Line 10: j004
      
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Mar 30, 2023#3

        Thank you for the response!

        Sure, below is the source before the script and the results I'm looking for after. I think I was complicating it by replacing GO TO by GO TO PERFORM. What I need to search on is any string that's between GO TO and -EXIT.

        Code: Select all

        perform j000-main thru j000-exit
        go to j000-exit
        
        perform j001-main thru j001-exit
        
        
        perform j002-main 
        go to j002-exit
        
        perform j003-main 
        
        
        perform j004-main thru j004-exit
        go to j004-exit
        Output after script. Ideally in a separate file so I can do a search on it.

        Code: Select all

        perform j000-main thru j000-exit
        go to j000-exit
        
        perform j002-main 
        go to j002-exit
        
        perform j004-main thru j004-exit
        go to j004-exit
        

        6,686585
        Grand MasterGrand Master
        6,686585

          Mar 30, 2023#4

          The script below produces the wanted output for the input sample and saves the active file on modification as new file.

          Please read the comments in the script (strings after //) for the description of the script. I assume that a COBOL programmer understands at least the comments.

          Code: Select all

          if (UltraEdit.document.length > 0)  // Is any file opened?
          {
             // Define the environment for this script.
             UltraEdit.insertMode();
             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
          
             // Is the active file a named file with a file name?
             if (!UltraEdit.activeDocument.isName(""))
             {
                UltraEdit.save();    // Save the active, named file. A new, unsaved
             }                       // file is not saved before processing it.
          
             // Move caret to top of the active file.
             UltraEdit.activeDocument.top();
          
             // Clear the active output window.
             UltraEdit.outputWindow.clear();
          
             // Define the parameters for the case-insensitive Perl regular expression finds.
             UltraEdit.perlReOn();
             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;
             if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
             {
                UltraEdit.activeDocument.findReplace.searchInColumn=false;
             }
          
             // Variable for the information about the deletion of at least one procedure.
             var bProcedureDeleted = false;
          
             // Search in a loop for a line containing "perform" at beginning of the line
             // after zero or more tabs/spaces and select everything up to first hyphen.
             while (UltraEdit.activeDocument.findReplace.find("^[\\t ]*perform[\\t ].+?-"))
             {
                // Store the line number of the active line in the file.
                var sLineNumber = UltraEdit.activeDocument.currentLineNum;
          
                // Get the procedure name after "perform" before the hyphen.
                var sProcedureName = UltraEdit.activeDocument.selection.replace(/^[\t ]*perform[\t ]+(.+)-$/i,"$1");
          
                // Search for a line beginning either with "go to" and the procedure
                // name and "-exit" or another procedure beginning with perform.
                if (UltraEdit.activeDocument.findReplace.find("^[\\t ]*(?:go to[\\t ]+"+sProcedureName+
                                                              "-exit|perform[\\t ].+?-)"))
                {
                   // Is the found and selected text again a beginning of a procedure?
                   if (UltraEdit.activeDocument.selection.search(/^[\t ]*\bperform\b/i) >= 0)
                   {
                      // The previously found procedure does not have a "go to *-exit"
                      // and is for that reason deleted from the active file. Move the
                      // caret to beginning of the new procedure line, select everything
                      // up to beginning of previous procedure line, delete the selected
                      // text and record that procedure deletion in the output window.
                      bProcedureDeleted = true;
                      UltraEdit.activeDocument.gotoLine(0,1);
                      UltraEdit.activeDocument.gotoLineSelect(sLineNumber,1);
                      UltraEdit.activeDocument.deleteText();
                      UltraEdit.outputWindow.write("Deleted procedure: " + sProcedureName);
                   }
                }
                else
                {
                   // The last procedure in the file does not have a "go to *-exit".
                   // Delete everything from beinning of the procedure to end of file.
                   bProcedureDeleted = true;
                   UltraEdit.activeDocument.gotoLine(sLineNumber,1);
                   UltraEdit.activeDocument.selectToBottom();
                   UltraEdit.activeDocument.deleteText();
                   UltraEdit.outputWindow.write("Deleted procedure: " + sProcedureName);
                }
             }
          
             if (bProcedureDeleted)  // Is any procedure deleted?
             {
                // Get name of active and modified file and character index
                // position of last dot and last backslash in the file name,
                var sFileName = UltraEdit.activeDocument.path;
                var nLastDot = sFileName.lastIndexOf(".");
                var nLastBackslash = sFileName.lastIndexOf("\\");
          
                if (nLastDot > nLastBackslash)   // Has the active file a file extension.
                {
                   // Insert "_new" left to the file extension for the new file name.
                   sNewFileName = sFileName.substring(0,nLastDot);
                   sNewFileName += "_new";
                   sNewFileName += sFileName.substring(nLastDot);
                }
                else
                {
                   // Append "_new" to the name of active file without file extension.
                   sNewFileName = sFileName + "_new";
                }
                // Save the active and modified file with the new file name.
                UltraEdit.saveAs(sNewFileName);
          
                // Show the output window to get displayed the deleted procedures.
                UltraEdit.outputWindow.showStatus=false;
                UltraEdit.outputWindow.showWindow(true);
             }
          
             // Move caret to top of the file.
             UltraEdit.activeDocument.top();
          }
          
          Best regards from an UC/UE/UES for Windows user from Austria

          3
          NewbieNewbie
          3

            Mar 31, 2023#5

            I was able to get the results I was looking for with the following script.
            Thank you so much for your assistance! There's no way I would've gotten this without your help lol

            Code: Select all

            if (UltraEdit.document.length > 0)  // Is any file opened?
            {
               // Define the environment for this script.
               UltraEdit.insertMode();
               if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
               else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
            
               // Move caret to top of the active file.
               UltraEdit.activeDocument.top();
            
               // Clear the active output window.
               UltraEdit.outputWindow.clear();
            
               // Define the parameters for the case-insensitive Perl regular expression finds.
               UltraEdit.perlReOn();
               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;
               if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
               {
                  UltraEdit.activeDocument.findReplace.searchInColumn=false;
               }
            
               // Variable to count found items
               i = 0;
               
               // Start with nothing on the clipboard
               UltraEdit.clearClipboard();
               
               // Search in a loop for a line containing "perform" at beginning of the line
               // after zero or more tabs/spaces and select everything up to first hyphen.
               while(UltraEdit.activeDocument.findReplace.find("^[\\t ]*perform[\\t ].+?-")) {
               
                     // Store the line number of the active line in the file.
                     var sLineNumber = UltraEdit.activeDocument.currentLineNum;
                     
                     // Get the procedure name after "perform" before the hyhphen.
                     var sProcedureName = UltraEdit.activeDocument.selection.replace(/^[\t ]*perform[\t ]+(.+)-$/i,"$1");
                
                     // Search for a line beginning either with "go to" and the procedure name and "-exit"
                     if (UltraEdit.activeDocument.findReplace.find("^[\\t ]*(?:go to[\\t ]+"+sProcedureName+
                                                                   "-exit)"))
                        {
                            UltraEdit.activeDocument.gotoLineSelect(sLineNumber,1);
                            UltraEdit.activeDocument.selectLine();
                            UltraEdit.activeDocument.cutAppend();
                            i++;
                        }
               }
               
               if (i > 0) {
                    // Create a new file for the report
                    UltraEdit.newFile();
                    UltraEdit.activeDocument.paste();
                    UltraEdit.activeDocument.top();
                  } 
            }

            6,686585
            Grand MasterGrand Master
            6,686585

              Apr 01, 2023#6

              Congratulation!

              It is really the first time that a user could solve a task with an UltraEdit script by modifying perfect a script written by me not being 100% what the user wanted. 👏
              Best regards from an UC/UE/UES for Windows user from Austria