Select lines beginning with period and copy to new document

Select lines beginning with period and copy to new document

23
Basic UserBasic User
23

    Jun 09, 2008#1

    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");
    

    236
    MasterMaster
    236

      Jun 10, 2008#2

      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)"

      21
      Basic UserBasic User
      21

        Jun 10, 2008#3

        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();
        if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
        else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.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, "" );
        

        262
        MasterMaster
        262

          Jun 10, 2008#4

          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 */

          6,610548
          Grand MasterGrand Master
          6,610548

            Jun 10, 2008#5

            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();
                  if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
                  else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.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!");
            Best regards from an UC/UE/UES for Windows user from Austria

            7
            NewbieNewbie
            7

              Aug 29, 2013#6

              I am looking for something very similar to this and hoping someone can help me. I write ruby scripts in UltraEdit and I comment what I am doing with a line that starts with a # (ruby comment character). I want to copy those lines to a new file, so that I can write up documentation for the script, what it does and how it works. I would also like to put the line number with the comment in the new file.

              Would this be hard to add? I am open to the easiest way to do it. My thought would be that any comment lines, starting with # would be copied to a new file with the line number before the copy.

              for example, if line 3 of the file has a comment #DEFINITION TO DO BLAH BLAH BLAH and line 10 has a comment #DEFINITION TO DO FOO BAR BLAH

              The new file would have:

              line 3 #DEFINITION TO DO BLAH BLAH BLAH
              line 10 #DEFINITION TO DO FOO BAR BLAH

              The other question I have is how I take one of these scripts, like the one posted in this thread, and get it to run in UE. I have never gotten into macros or scripts inside UE. I know they are powerful, but I haven't learned how to write them or use them much.

              Thanks!

              6,610548
              Grand MasterGrand Master
              6,610548

                Aug 29, 2013#7

                The script you need is already written.

                You need to add the script FindStringsWithLineNumbers.js from topic Find strings with a regular expression and output them to new file to Scripting - Scripts. Then open your Ruby script and run the script FindStringsWithLineNumbers.js from the scripts list.

                First, you are asked for the regular expression search string where you have to enter ^#.+$

                Second you are asked for line information pattern where you enter line # (with a space at end) or you hit just button OK if output format Lx: is also okay for you.

                Of course you can modify the script to not ask you on execution for the regular expression search string and line information pattern. There are some string variables defined starting on line 100 of the script. As the comments explain, initializing the variables sSearchString and sLineInfoPattern with a none empty string avoids asking you on script execution for the search regular expression and the line information pattern.

                var sSearchString = "^#.+$";
                var sLineInfoPattern = "line # ";

                7
                NewbieNewbie
                7

                  Aug 29, 2013#8

                  This worked fantastically. Thank you so much! :D