Tapatalk

The same lines counting

The same lines counting

2
NewbieNewbie
2

    Sep 04, 2009#1

    Hello,

    Is possible to count the same lines using regular expressions?
    For example, I want to count the same lines in this text

    Code: Select all

    trahrrnqw444
    trahrrnqw444
    trahrrnqw444
    tzbnntehraerath
    atebarbnr
    atebarbnr
    atebarbnr
    bcntyjy
    fgxnytj6y5t
    fgxnytj6y5t
    fgxnytj6y5t
    fgxnytj6y5t
    fgxnytj6y5t
    fgxnytj6y5t
    fgxnytj6y5t
    fgxnytj6y5t
    fgxnrys
    nrysyrsj5
    nrysyrsj5
    nrysyrsj5
    nrysyrsj5
    gfnjyjy5e
    gfnjyjy5e
    gfnjyjy5e
    gfnjyjy5e
    gfnjyjy5e
    gfnjyjy5e
    gfnjyjy5e
    gfnjyjy5e
    And to do something like this:

    Code: Select all

    1...trahrrnqw444
    2...trahrrnqw444
    3...trahrrnqw444
    1...tzbnntehraerath
    1...atebarbnr
    2...atebarbnr
    3...atebarbnr
    1...bcntyjy
    1...fgxnytj6y5t
    2...fgxnytj6y5t
    3...fgxnytj6y5t
    4...fgxnytj6y5t
    5...fgxnytj6y5t
    6...fgxnytj6y5t
    7...fgxnytj6y5t
    8...fgxnytj6y5t
    1...fgxnrys
    1...nrysyrsj5
    2...nrysyrsj5
    3...nrysyrsj5
    4...nrysyrsj5
    1...gfnjyjy5e
    2...gfnjyjy5e
    3...gfnjyjy5e
    4...gfnjyjy5e
    5...gfnjyjy5e

    16
    Basic UserBasic User
    16

      Sep 05, 2009#2

      A regex is not the right tool for the job. The best you can do with a regex is match multiple lines that are identical. Assuming you are dealing with DOS/Windows line terminations ("\r\n"), Here is a perl style regex that does just that:

      Code: Select all

      ^(.*\r\n)\1*
      But you don't need regex at all. Here is a script that does the trick.

      Code: Select all

      // File CountIdenticalLines.js
      UltraEdit.activeDocument.selectAll();            // select whole file contents
      var text = UltraEdit.activeDocument.selection;   // whole file contents
      if (text == '') exit();                          // exit if there is nothing to do
      var lines = text.split("\r\n");                  // whole original file as array of lines
      var newlines = [];                               // array to hold new concatenated lines
      var dupcnt = 1;                                  // count of duplicate lines
      for (var i = 0; i < lines.length; i++) {         // loop through all lines in file
          if ((i > 0) && (lines[i] == lines[i-1])) {   // if this line same as last
              dupcnt++;                                // increment to dup count
          } else {                                     // otherwise
              dupcnt = 1;                              // reset count back to 1
          }
          newlines[i] = dupcnt + "..." + lines[i];     // prepend dup count to line
      }                                                // loop for all lines
      text = newlines.join("\r\n");                    // concat all lines
      UltraEdit.activeDocument.write(text);            // and write back to file (replacing whole selection)

      2
      NewbieNewbie
      2

        Sep 07, 2009#3

        Thanks, I will try this.