How to join lines in a loop with an UltraEdit script?

How to join lines in a loop with an UltraEdit script?

3
NewbieNewbie
3

    Sep 30, 2019#1

    Hi brain-trust,

    I'm trying to create a script that will grab 1000 lines at a time and then use the join function. Then rinse and repeat to the end. I'm really bad at programming so I just thought I'd ask to see if some wonderful person could throw this together for me rather than me beating my head on my desk trying to figure it out. 😀

    Thanks!

    6,605548
    Grand MasterGrand Master
    6,605548

      Oct 02, 2019#2

      The command Join lines is not available as scripting command in UltraEdit for Windows v26.20.0.6. There is only the macro command JoinLines. However, it is no problem to code an UltraEdit script to select in a loop 1000 lines until end of file is reached and join the lines by using a replace all on selected text to remove all line terminations or replace them by a space.

      How do you want to join the lines?

      Is it okay to first trim all trailing spaces/tabs from all lines before starting the loop to join the lines?

      Is it okay to first remove all leading spaces/tabs from all lines before starting the loop to join the lines?

      Is it okay to remove all empty lines before starting the loop to join the lines?

      Should lines joining be done with replacing line termination by a space?

      That's how the command Join lines joins the lines, with the exception of the leading spaces/tabs on first line and the trailing spaces/tabs on last line on selection ending before line termination on last line. But you want perhaps a different behavior for joining the lines which could be easily coded in script, too.
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Oct 02, 2019#3

        I'd like it to work exactly like the command does, so it doesn't need to trim anything, but put in the space.

        Thanks!

        6,605548
        Grand MasterGrand Master
        6,605548

          Oct 03, 2019#4

          The first script selects 1000 lines in a loop and runs a Perl regular expression replace all on selection to replace (nearly) all whitespaces around a line termination by a space character which is the same as executing command Join lines on this selection for files not containing special Unicode whitespaces.

          Code: Select all

          var nLinesPerJoin = 1000;
          
          // Is any file opened and is number of lines to join greater than 1?
          if ((UltraEdit.document.length > 0) && (nLinesPerJoin > 1))
          {
             // Define environment for this script.
             UltraEdit.insertMode();
             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
          
             // \xA0 matches a no-break space.
             var sJoinSearch;
             if (UltraEdit.activeDocument.lineTerminator < 1)
             {
                sJoinSearch = "[\\t\\f \\xA0]*\\r\\n\\s*";
             }
             else if (UltraEdit.activeDocument.lineTerminator == 1)
             {
                sJoinSearch = "[\\t\\f \\xA0]*\\n\\s*";
             }
             else
             {
                sJoinSearch = "[\\t\\f \\xA0]*\\r\\s+";
             }
             UltraEdit.perlReOn();
             UltraEdit.activeDocument.findReplace.mode=1;
             UltraEdit.activeDocument.findReplace.matchCase=true;
             UltraEdit.activeDocument.findReplace.matchWord=false;
             UltraEdit.activeDocument.findReplace.regExp=true;
             UltraEdit.activeDocument.findReplace.searchDown=true;
             UltraEdit.activeDocument.findReplace.searchInColumn=false;
             UltraEdit.activeDocument.findReplace.selectText=true;
             UltraEdit.activeDocument.findReplace.preserveCase=false;
             UltraEdit.activeDocument.findReplace.replaceAll=true;
             UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
          
             // Join the lines which means selecting nLinesPerJoin - 1 lines
             // and replace in selection all whitespace characters around a
             // line termination with a space character.
             var nLineNumber = 1;
             nLinesPerJoin -= 1;
          
             while(!(UltraEdit.activeDocument.isEof()))
             {
                UltraEdit.activeDocument.gotoLine(nLineNumber,1);
                UltraEdit.activeDocument.gotoLineSelect(nLineNumber + nLinesPerJoin,1);
                if (!UltraEdit.activeDocument.isSel()) break;
                UltraEdit.activeDocument.startSelect();
                UltraEdit.activeDocument.key("END");
                UltraEdit.activeDocument.endSelect();
                UltraEdit.activeDocument.findReplace.replace(sJoinSearch," ");
                nLineNumber += 1;
             }
             UltraEdit.activeDocument.top();
          }
          
          The second script is a bit different. It first removes all trailing and leading normal spaces and horizontal tabs and next deletes all empty lines before joining 1000 lines per loop iteration by replacing each line termination in selected block by a space character using a non-regular expression replace.

          Code: Select all

          var nLinesPerJoin = 1000;
          
          // Is any file opened and is number of lines to join greater than 1?
          if ((UltraEdit.document.length > 0) && (nLinesPerJoin > 1))
          {
             // Define environment for this script.
             UltraEdit.insertMode();
             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
          
             // Move caret to top of the active file.
             UltraEdit.activeDocument.top();
          
             // Remove normal spaces and horizontal tabs at beginning
             // and at end of every line and then also all empty lines.
             UltraEdit.activeDocument.trimLeadingSpaces();
             UltraEdit.activeDocument.trimTrailingSpaces();
             var sLineTerm;
             var sEmptyLines;
             if (UltraEdit.activeDocument.lineTerminator < 1)
             {
                sLineTerm = "^p";
                sEmptyLines = "\\r\\n\\K\\s+";
             }
             else if (UltraEdit.activeDocument.lineTerminator == 1)
             {
                sLineTerm = "^n";
                sEmptyLines = "\\n\\K\\s+";
             }
             else
             {
                sLineTerm = "^r";
                sEmptyLines = "\\r\\K\\s+";
             }
             UltraEdit.perlReOn();
             UltraEdit.activeDocument.findReplace.mode=0;
             UltraEdit.activeDocument.findReplace.matchCase=true;
             UltraEdit.activeDocument.findReplace.matchWord=false;
             UltraEdit.activeDocument.findReplace.regExp=true;
             UltraEdit.activeDocument.findReplace.searchDown=true;
             UltraEdit.activeDocument.findReplace.searchInColumn=false;
             UltraEdit.activeDocument.findReplace.selectText=false;
             UltraEdit.activeDocument.findReplace.preserveCase=false;
             UltraEdit.activeDocument.findReplace.replaceAll=true;
             UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
             UltraEdit.activeDocument.findReplace.replace(sEmptyLines,"");
          
             // Join the lines which means selecting nLinesPerJoin - 1 lines
             // and replace in selection every line termination by a space.
             var nLineNumber = 1;
             nLinesPerJoin -= 1;
          
             UltraEdit.activeDocument.findReplace.mode=1;
             UltraEdit.activeDocument.findReplace.regExp=false;
             UltraEdit.activeDocument.findReplace.selectText=true;
          
             while(!(UltraEdit.activeDocument.isEof()))
             {
                UltraEdit.activeDocument.gotoLine(nLineNumber,1);
                UltraEdit.activeDocument.gotoLineSelect(nLineNumber + nLinesPerJoin ,1);
                if (!UltraEdit.activeDocument.isSel()) break;
                UltraEdit.activeDocument.findReplace.replace(sLineTerm," ");
                nLineNumber += 1;
             }
             UltraEdit.activeDocument.top();
          }
          
          Both scripts were tested by me with UltraEdit for Windows v26.20.0.6 on a copy of file changes.txt in program files folder of UltraEdit which has empty lines and lots of lines with leading spaces.
          Best regards from an UC/UE/UES for Windows user from Austria

          3
          NewbieNewbie
          3

            Oct 03, 2019#5

            OMG, thanks so much!