Select lines beginning with period and copy to new document

Help with writing and running scripts

Select lines beginning with period and copy to new document

Postby caveatrob » Mon Jun 09, 2008 3:17 pm

Hello Everyone,

I'm working on code that will find occurences of strings that begin with "." and copy them to a new file.

The piece I'm working on now finds the lines that begin with a period, though it doesn't seem to work. The regex finds *all* occurences of periods.

Code below:

Code: Select all

UltraEdit.perlReOn();
UltraEdit.activeDocument.findReplace.regExp = true;
UltraEdit.activeDocument.findReplace.replaceAll = true;
var regexpFind = "^\.";
UltraEdit.activeDocument.top();
//UltraEdit.activeDocument.findReplace.find(regexpFind);
while ( (UltraEdit.activeDocument.findReplace.find(regexpFind)) && ! UltraEdit.activeDocument.isEof() )
{
   //UltraEdit.outputWindow.write("found")
   renumberValue++;   
}


UltraEdit.outputWindow.write("Value is:"+renumberValue);


//UltraEdit.newFile();
//UltraEdit.activeDocument.top();
//UltraEdit.activeDocument.write("hi");
caveatrob
Basic User
Basic User
 
Posts: 20
Joined: Fri Apr 25, 2008 4:39 pm

Re: Select lines beginning with period and copy to new document

Postby pietzcker » Tue Jun 10, 2008 1:25 am

I'm not sure but I'd wager it is because you need to escape the backslash in a JavaScript string, i. e. use

var regexpFind="^\\."

If you want to capture and use the content of the line, the regex would be

^(\..*\r\n)

(assuming it's a DOS/Windows file with CRLF newlines, and that you want to capture the line together with its newlines).

So in the script it should be

var regexpFind="^(\\..*\\r\\n)"
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Select lines beginning with period and copy to new document

Postby Jaretin » Tue Jun 10, 2008 1:32 am

I think the find-function is buggy again.
And as pietzcker said, your regexpFind is wrong, because the \ has to be escaped in the string.

The following Code will work (not very nice, but it will do it).
Code: Select all
UltraEdit.insertMode();
UltraEdit.hexOff;
UltraEdit.perlReOn();
UltraEdit.columnModeOff();

var regexpFind = "^\\.";
var lastLine = -1;
var renumberValue = 0;
UltraEdit.activeDocument.findReplace.regExp = true;
UltraEdit.activeDocument.top();
while ( (UltraEdit.activeDocument.findReplace.find(regexpFind) ) )
{
   if ( lastLine == UltraEdit.activeDocument.currentLineNum )
      break;
UltraEdit.messageBox(""+UltraEdit.activeDocument.currentLineNum+"|"+UltraEdit.activeDocument.currentColumnNum+"|"+UltraEdit.activeDocument.selection.length);
   if ( UltraEdit.activeDocument.isEof() )
      break;
      lastLine = UltraEdit.activeDocument.currentLineNum;
      renumberValue++;
}
UltraEdit.messageBox("" + renumberValue, "" );
User avatar
Jaretin
Basic User
Basic User
 
Posts: 19
Joined: Sun Mar 25, 2007 11:00 pm

Re: Select lines beginning with period and copy to new document

Postby jorrasdk » Tue Jun 10, 2008 2:43 am

Here is my short version of "find lines with pattern and paste into new document" as a script - I hope the comments in the code are enough explanation:

Code: Select all
/* Help prototype function to remove starting and ending / as UE does not recognize these for regexp */
RegExp.prototype.toUEregexp = function() { return this.toString().replace(/^\/|\/$/g, ""); };

UltraEdit.perlReOn(); /* use perl regex engine */
UltraEdit.activeDocument.findReplace.regExp = true; /* find using regexp, handle other defaults of findReplace here */

var regexpFind=/^\..*\r\n/; /* use a regexp object and not a string, so we don't have to double-escape backslashes */

UltraEdit.activeDocument.top();
var clipboardIdx = UltraEdit.clipboardIdx; /* remember active clipboard */
UltraEdit.selectClipboard(9); /* use clipboard 9 */
UltraEdit.clearClipboard(); /* empty clipboard 9 */
while ( UltraEdit.activeDocument.findReplace.find(regexpFind.toUEregexp() )) { /* find lines and append */
   UltraEdit.activeDocument.copyAppend();
}
UltraEdit.newFile(); /* open new file */
UltraEdit.activeDocument.paste(); /* paste copyAppeded lines */
UltraEdit.clearClipboard(); /* clear clipboard 9 */
UltraEdit.selectClipboard(clipboardIdx); /* switch back to original clipboard */
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Select lines beginning with period and copy to new document

Postby Mofi » Tue Jun 10, 2008 3:29 am

Here is my version which is based on the version of jorrasdk, but contains some additional checks.

Code: Select all
// Is any file currently open?
if (UltraEdit.document.length > 0) {
   UltraEdit.perlReOn();
   UltraEdit.activeDocument.hexOff();
   UltraEdit.activeDocument.bottom();
   // Is the last line of the file terminated with a line ending?
   if (UltraEdit.activeDocument.isColNumGt(1)) {
      // No, insert missing line ending (DOS, UNIX or MAC).
      UltraEdit.insertMode();
      UltraEdit.columnModeOff();
      UltraEdit.activeDocument.insertLine();
      // If auto indent is enabled and last line starts with white-spaces,
      // delete the automatically inserted white-spaces in the new last line.
      if (UltraEdit.activeDocument.isColNumGt(1)) {
         UltraEdit.activeDocument.deleteToStartOfLine();
      }
   }
   UltraEdit.activeDocument.top();
   var ActiveClipboard = UltraEdit.clipboardIdx;
   UltraEdit.selectClipboard(9);
   UltraEdit.clearClipboard();
   UltraEdit.activeDocument.findReplace.mode=0;
   UltraEdit.activeDocument.findReplace.searchDown=true;
   UltraEdit.activeDocument.findReplace.matchCase=false;
   UltraEdit.activeDocument.findReplace.matchWord=false;
   UltraEdit.activeDocument.findReplace.regExp=true;
   var bFound = false;
   while (UltraEdit.activeDocument.findReplace.find("^\\..*\\r*\\n")) {
      UltraEdit.activeDocument.copyAppend();
      bFound = true;
   }
   UltraEdit.activeDocument.top();
   if (bFound) {
      UltraEdit.newFile();
      UltraEdit.activeDocument.paste();
      UltraEdit.activeDocument.top();
      UltraEdit.clearClipboard();
   } else UltraEdit.messageBox("There is no line starting with a period!");
   UltraEdit.selectClipboard(ActiveClipboard);
} else UltraEdit.messageBox("You should have a file open when you run this script!");
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts