Delete line at position x to end

Delete line at position x to end

3
NewbieNewbie
3

    13:58 - Nov 29#1

    Hi experts,
    last time Mofi helped me to develop this code for repacing 0000 to XX00 at the beginning of a line:

    Code: Select all

    UltraEdit.activeDocument.top();
    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();
    
       // Move caret to top of the active file.
       UltraEdit.activeDocument.top();
    
       // Define all parameters for an UltraEdit regular expression replace
       // all in entire active file searching for the string 0000 at beginning
       // of a line and replace each found occurrence with the string AB.
       UltraEdit.activeDocument.findReplace.mode=0;
       UltraEdit.activeDocument.findReplace.matchCase=true;
       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;
       UltraEdit.ueReOn();     // UltraEdit regular expression engine
       UltraEdit.activeDocument.findReplace.replace("%0000", "XX");
    }
    Now I want to add new code that deletes the rest of each line at position 15 to the end (of line).
    I asked ChatGPT and this was the answer:

    Code: Select all

    UltraEdit.activeDocument.top();
    
    while (!UltraEdit.activeDocument.isEof()) {
        if (UltraEdit.activeDocument.currentPos > 14) {
            UltraEdit.activeDocument.gotoLine(UltraEdit.activeDocument.currentLineNum, 14);
            UltraEdit.activeDocument.selectToEOL();
            UltraEdit.activeDocument.deleteSelection();
        }
        UltraEdit.activeDocument.key("DOWN");
    }
    But, I don't know how to integrate this new code.
    Maybe you can help me?

    6,687587
    Grand MasterGrand Master
    6,687587

      13:58 - Nov 30#2

      The code generated by ChatGPT is nonsense.

      There must be just changed one line and one inserted to get first deleted from all lines everything after the first 14 characters and then replace 0000 by XX at the beginning of lines. Four comment lines are additionally modified.

      Code: Select all

      UltraEdit.activeDocument.top();
      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();
      
         // Move caret to top of the active file.
         UltraEdit.activeDocument.top();
      
         // Define all parameters for a Perl regular expression replace all in
         // entire active file searching for the string 0000 at beginning of a
         // line and replacing each found occurrence with the string XX after
         // deleting all characters from all lines after the first 14 characters.
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         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;
         UltraEdit.perlReOn();   // Perl regular expression engine
         UltraEdit.activeDocument.findReplace.replace("^.{14}\\K.*$", "");
         UltraEdit.activeDocument.findReplace.replace("^0000", "XX");
      }
      
      Input example:

      Code: Select all

      00000012345678 . . .
      00000001234567 . . .
      00000000123456 . . .
      00000012345678 . . .
      Result of the script for the input example:

      Code: Select all

      XX0012345678
      XX0001234567
      XX0000123456
      XX0012345678
      Best regards from an UC/UE/UES for Windows user from Austria