Script to delete all but last N lines

Script to delete all but last N lines

51
NewbieNewbie
51

    Dec 07, 2017#1

    I am not familiar with UltraEdit scripts, but I would like to write a script that does the following.

    1) Prompts the user for the number of lines to keep, say stored in a variable N.
    2) Delete any blank lines first.
    3) Starting from the top of the file, delete all but the last N records.

    The reason for this is that I have a process that reads and processes records starting from the top of the file. The process stalls out sometimes, and I need to restart the process with the pending records only.

    6,604547
    Grand MasterGrand Master
    6,604547

      Dec 07, 2017#2

      Which version of UltraEdit on which operating system do you use?

      There are several features added in UltraEdit for Windows like real support for Perl regular expression escape character \z (end of buffer being interpreted as end of file) or the macro command DelAllEmptyLines introduced with UE for Windows v24.20.0.40.

      Well, please be also clear on what you mean with Delete any blank lines first because there is a big difference between an empty line and a blank line, see post Remove / delete blank and empty lines. Deletion of lines containing only whitespace characters which are non empty, but blank lines, require a regular expression replace. The command to delete empty lines deletes really only empty lines.

      Is the last line in file always terminated with a line ending or can that vary?

      Is the line ending type always the same and if the answer is yes which one?
      Best regards from an UC/UE/UES for Windows user from Austria

      51
      NewbieNewbie
      51

        Dec 07, 2017#3

        I am using UltraEdit Professional Text/HEX Editor Version 23.20.0.34 on a Windows 7 Professional Machine. All lines are terminated in hex 0D0A. The last line of the file is the only empty line as it follows the last 0D0A. I guess it really doesn't need to be removed, but I wanted the line count to match the actual number of lines. See the attached pictures. The input file shows 11 lines, with the last one being empty. If I only wanted the last 4 lines, the output file shows 4 lines (lines 7 through 10) of the input file. My actual files are thousands of lines long, so that is why I wanted to use a macro for this. Thank you for your reply.
        OutputFile.jpg (69.35KiB)
        InputFile.jpg (50.18KiB)

        6,604547
        Grand MasterGrand Master
        6,604547

          Dec 07, 2017#4

          There is no more line when a file ends with a line ending. It is possible to set the caret after last line ending in file to be able to append characters at end of file. But technically there is no line after last carriage return + line-feed at end of a file. If a text file ends with other characters than newline characters, the file does not end with a line, but with a string at end of file being displayed by text editors in a separate line as there is no other method to display that string at end of file.

          Why is this important?

          UltraEdit and Unix regular expression engines find a string at end of a line with a regular expression ending with $ on last line of a file only when there is really a line ending at end of file. Only Perl regular expression engine interprets on usage of $ also end of file as end of line.

            Dec 07, 2017#5

            Here is the script code for this task.

            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();
            
               // Prompt user for number of lines to keep in a loop
               // until a positive number was entered by the user.
               do
               {
                  var nLinesToKeep = UltraEdit.getValue("Number of lines to keep",1);
               }
               while(nLinesToKeep < 1);
            
               // Move caret to end of active file.
               UltraEdit.activeDocument.bottom();
            
               // Get line number and column number at end of file.
               var nLineNumber = UltraEdit.activeDocument.currentLineNum;
            
               // Subtract the number of lines to keep if the file ends with
               // a line ending, or number of lines to keep decremented by 1
               // if the file has no line ending at end of file.
               if (UltraEdit.activeDocument.currentColumnNum > 1) nLinesToKeep--;
               nLineNumber -= nLinesToKeep;
            
               // The line number must be greater than 1 to select
               // and delete anything at all from active file.
               if(nLineNumber > 1)
               {
                  UltraEdit.activeDocument.gotoLine(nLineNumber,1);
                  UltraEdit.activeDocument.selectToTop();
                  UltraEdit.activeDocument.deleteText();
               }
            
               // Move caret to start of active file.
               UltraEdit.activeDocument.top();
            }
            
            Please note that this script does not work if line counting is disabled at Advanced - Settings or Configuration - Editor Display - Miscellaneous by checking Disable line numbers which is helpful on editing very large files with hundreds of MiB or even GiB. This setting is not checked by default as it can be seen in status bar on getting a line number displayed. That's the reason why I thought about using a regular expression approach to delete all lines except the last N lines which would work independent on this configuration setting.

            One more note: There is a Post Reply button at top and at bottom of a topic page  to post a reply without quoting an existing post. The Reply with quote button inside a post should be not used in general.
            Best regards from an UC/UE/UES for Windows user from Austria