Find the maximum length of a line in file

Find the maximum length of a line in file


    Aug 09, 2016#1


    I am new to UltraEdit. I try to replace a text from list of '.PRT' files. This '.PRT' file is used by Mainframe application.
    When I do that at the end of replace I want to know the length of the longest line. This file should of fixed length. Hence by knowing the length before and after the change I have to move some text to next line so that the maximum line length do not exceed the length defined. Is it possible to get the longest line length by any means UltraEdit for the file (list of files) selected.

    Grand MasterGrand Master

      Aug 09, 2016#2

      See Function to get list of files into an edit window for function GetListOfFiles and how to use it to run a script on all files of a directory or directory tree matching a file name pattern.

      There are the topics Sort by line (column) length (old macro solution) and Find and goto longest line (newer scripting solution).

      It would be also possible to code a script which runs Perl regular expression Find in Files (from a high length value to lower length values until longest line found) to determine the longest line in a set of files.

      For small files which can be loaded completely in memory of JavaScript core on running an UltraEdit script it is also possible to use this script code to find out the length (= number of characters) of longest line in file.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
         var nMaximumLineLength = 0;
         // Define environment for this script.
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         // Determine type of line termination (DOS/UNIX/MAC).
         var sLineTerm = "\r\n";
         if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
         else if (UltraEdit.activeDocument.lineTerminator == 2) sLineTerm = "\r";
         // Get all lines from active file into memory as an array of strings
         // and compare the number of characters of each line with the current
         // maximum value to get finally the length of the longest line in file.
         if (UltraEdit.activeDocument.isSel())  // Is the file not empty?
            var asLines = UltraEdit.activeDocument.selection.split(sLineTerm);
            for (var nLine = 0; nLine < asLines.length; nLine++)
               if (asLines[nLine].length > nMaximumLineLength)
                  nMaximumLineLength = asLines[nLine].length;
  ;  // Cancels the selection.
         UltraEdit.messageBox("Maximum line length is: " + nMaximumLineLength);
      If the file contains horizontal tabs and the tab stop value must be taken into account on determining the length of longest line in a file, non of the scripting solutions work as they all work with the number of characters within a line and not with length of line as displayed in UltraEdit taking horizontal tabs and tab stop value(s) into account.
      Best regards from an UC/UE/UES for Windows user from Austria