The same lines counting

Help with writing and running scripts

The same lines counting

Postby m1000 » Fri Sep 04, 2009 6:53 am

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
m1000
Newbie
 
Posts: 2
Joined: Fri Sep 04, 2009 6:51 am

Re: The same lines counting

Postby ridgerunner » Sat Sep 05, 2009 12:39 pm

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)
User avatar
ridgerunner
Basic User
Basic User
 
Posts: 18
Joined: Thu Sep 15, 2005 11:00 pm
Location: SLC, UT USA

Re: The same lines counting

Postby m1000 » Mon Sep 07, 2009 8:57 am

Thanks, I will try this.
m1000
Newbie
 
Posts: 2
Joined: Fri Sep 04, 2009 6:51 am


Return to Scripts