Tapatalk

Delete line at position x to end

Delete line at position x to end

4
NewbieNewbie
4

    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,685587
    Grand MasterGrand Master
    6,685587

      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

      4
      NewbieNewbie
      4

        17:47 - 19 days ago#3

        Hi Mofi,
        thanks for your support!
        With the code above I can't get your result.

        The original text looks like this:

        Code: Select all

        00000012345678;this is an error; the error number is: 404;
        00000087654321;this is an error; the error number is: 404;
        . . .
        Only the two last lines are important:

        Code: Select all

        UltraEdit.activeDocument.findReplace.replace("^.{14}\\K.*$", "");
        UltraEdit.activeDocument.findReplace.replace("^0000", "XX");
        When I have changed the first line to:

        Code: Select all

        UltraEdit.activeDocument.findReplace.replace("^;{14}\\K.*$", "");
        the second line was processed: XX at the beginning of each line - the rest of the line wasn't deleted.

        To put this line

        Code: Select all

        UltraEdit.activeDocument.top();
        between the two last lines wasn't successful.

        Should be there a pause (sleep) or anything else?
        How does UltraEdit works at this position?

        6,685587
        Grand MasterGrand Master
        6,685587

          12:20 - 18 days ago#4

          In the script posted by me the regular expression engine is changed from UltraEdit to Perl which of course is also very important.

          You have not used the search expression as posted by me. I posted the Perl regular search expression: ^.{14}\K.*$

          ^ … start each search at the beginning of the line.

          .{14} … the dot matches any character except new line characters and there must be fourteen characters matched before a newline character is found.

          You replaced the dot by a semicolon which has no special meaning and kept the multiplier. The modified expression searches therefore for a line beginning with fourteen semicolons and finds no such line.

          \K … keep back the already matched string, i.e. ignore the first fourteen found characters for the replace.

          .* … find any character except newline characters zero or more times.

          $ ... stop matching characters at the end of the line without matching (selecting) also the newline character(s).

          Your modification of regular expression character . to a semicolon is the reason why this replace for deleting everything after the first fourteen characters on every line does not work as expected by you.

          There could be also used the Perl regular search expression ;.*$ for searching anywhere in file for a semicolon and matching it, matching next zero or more characters except newline characters to end of the line (or file) and replacing all matched characters by an empty string. That results in deleting everything from first semicolon found in a line to end of the line in each line of the file.

          The legacy UltraEdit regular expression engine does not support multipliers like {14} or special expressions like \K for keeping back parts of matched string.
          Best regards from an UC/UE/UES for Windows user from Austria