Count repetitive numbers/lines and report the sum of each group

Count repetitive numbers/lines and report the sum of each group

74
Advanced UserAdvanced User
74

    Jan 09, 2014#1

    Is it possible to create a script to count repetitive numbers and report the sum of each group?

    For example:
    250
    250
    250
    300
    300
    90
    800
    800
    800

    Should create this report
    3: 250
    2: 300
    1: 90
    3: 800

    The numbers are line by line in the file. I'll edit out the string XXXX-MM-320 to just the number. But each number will fall on a new line.

    6,686585
    Grand MasterGrand Master
    6,686585

      Jan 11, 2014#2

      This small and simple UltraEdit script reports in a new file how often each line exists in active file.

      It does not matter if the file contains just a number on each line, or just one word per line, or terms. So it can be used to count nearly everything which is written into a text file line by line.

      The only limitation is the number of available RAM for JavaScript core engine inside UltraEdit, which is most often not equal the available RAM for all applications, as the script loads all lines into memory for running the string compares and counting the occurrences of each line.

      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();
      
         // Select entire file content.
         UltraEdit.activeDocument.selectAll();
         // Ignore empty files detected by nothing selected.
         if (UltraEdit.activeDocument.isSel())
         {
            // Get type of line terminator: DOS / UNIX / MAC.
            var sLineTerm = "\r\n";
            if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
            else if (UltraEdit.activeDocument.lineTerminator == 2) sLineTerm = "\r";
      
            // Get the selected lines into an array of strings with each string
            // being one line and cancel the selection with moving caret to top.
            var asLines = UltraEdit.activeDocument.selection.split(sLineTerm);
            UltraEdit.activeDocument.top();
      
            // Remove last string of the array if empty because
            // active file ends with the line terminator.
            if (asLines[asLines.length-1] == "") asLines.pop();
      
            // Compare each line with all other lines below, remove
            // duplicates and count how many duplicate lines exist.
            for (var nCompareLine = 0; nCompareLine < asLines.length; nCompareLine++)
            {
               var nLineCount = 1;
               var nCurrentLine = nCompareLine+1;
               while (nCurrentLine < asLines.length)
               {
                  // Is the number at current line identical
                  // with the number at the line to compare?
                  if (asLines[nCompareLine] != asLines[nCurrentLine])
                  {
                     nCurrentLine++;  // No, continue on next line.
                  }
                  else  // Yes, remove this duplicate line from the array and do
                  {     // not increase line index, but increase found counter.
                     asLines.splice(nCurrentLine,1);
                     nLineCount++;
                  }
               }
               // Insert the number of ocurrences of this line at
               // beginning of the line. Then continue with next line.
               asLines[nCompareLine] = nLineCount.toString(10) + ": " + asLines[nCompareLine];
            }
      
            // Create a new file.
            UltraEdit.newFile();
            // Get type of line terminator: DOS / UNIX / MAC.
            sLineTerm = "\r\n";
            if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
            else if (UltraEdit.activeDocument.lineTerminator == 2) sLineTerm = "\r";
      
            // Append an empty string to the remaining array of lines to terminate
            // finally also last line of the file with a line terminator.
            asLines.push("");
            // Join the lines in the array with line terminator of new file in
            // user clipboard 9 as pasting a large block is faster than writing
            // a large string (block) into a file depending on size of the block.
            UltraEdit.selectClipboard(9);
            UltraEdit.clipboardContent = asLines.join(sLineTerm);
            UltraEdit.activeDocument.paste();
            UltraEdit.activeDocument.top();
            UltraEdit.clearClipboard();
            UltraEdit.selectClipboard(0);  // Select system clipboard.
         }
      }
      Best regards from an UC/UE/UES for Windows user from Austria

      74
      Advanced UserAdvanced User
      74

        Jan 13, 2014#3

        OMG Mofi this is an awesome script! Thank you!

        1581
        Power UserPower User
        1581

          Mar 29, 2019#4

          Thanks Mofi - needed this too.
          UE 26.20.0.74 German / Win 10 x 64 Pro