How to duplicate all lines in a file X times?

How to duplicate all lines in a file X times?

1032
Power UserPower User
1032

    Jan 27, 2018#1

    I need the opposite of our colleague. I need a macro to duplicate same line a given number of times. It seems a very easy task, but I'm in troubles.

    As I searched, I guess there is no command to open some input window to give the number.
    Because I'm not expert to scripts, macro would be the first choice.

    Let say, I have this line:

    Code: Select all

    The Quick Brown Fox Jumps Over the Lazy Dog
    And I want to duplicate it 9 times to have this line in total 10 times in the file.

    Code: Select all

    The Quick Brown Fox Jumps Over the Lazy Dog
    The Quick Brown Fox Jumps Over the Lazy Dog
    The Quick Brown Fox Jumps Over the Lazy Dog
    The Quick Brown Fox Jumps Over the Lazy Dog
    The Quick Brown Fox Jumps Over the Lazy Dog
    The Quick Brown Fox Jumps Over the Lazy Dog
    The Quick Brown Fox Jumps Over the Lazy Dog
    The Quick Brown Fox Jumps Over the Lazy Dog
    The Quick Brown Fox Jumps Over the Lazy Dog
    The Quick Brown Fox Jumps Over the Lazy Dog
    I tried this:

    Code: Select all

    InsertMode
    ColumnModeOff
    HexOff
    Loop 0
    IfEof
    Key Ctrl+HOME
    ExitMacro
    Else
    PlayMacro 1 "Dup"
    EndIf
    EndLoop
    And macro Dup macro is:

    Code: Select all

    InsertMode
    ColumnModeOff
    HexOff
    StartSelect
    Key DOWN ARROW
    Copy
    EndSelect
    CancelSelect
    Paste
    Key DOWN ARROW
    
    Because I don't know how to stop the macro, I put Eof clause, but I know that it may leads to infinite loop.

    I also thought that I could compare clipboard number to stop the macro when incremented number reaches the value, but don't know how again.

    How to increment clipboard number?
    How to compare it inside the macro?
    Is there a way to call an input window?

    Any ideas?

    6,681583
    Grand MasterGrand Master
    6,681583

      Jan 28, 2018#2

      Run a Perl regular expression Replace All using a back-reference from top of file with search string ^(.*(:?\r?\n|\r))\K and replace string \1\1\1\1\1\1\1\1\1 which 9 times back-references the same found string kept unmodified in file.

      The last line in the file must have also a line ending as it is ignored otherwise by the search expression.

      This replace as macro code with additional commands making sure that last line of file has also a line termination.

      Code: Select all

      InsertMode
      ColumnModeOff
      HexOff
      Bottom
      IfColNumGt 1
      InsertLine
      IfColNumGt 1
      DeleteToStartofLine
      EndIf
      EndIf
      Top
      PerlReOn
      Find MatchCase RegExp "^(.*(:?\r?\n|\r))\K"
      Replace All "\1\1\1\1\1\1\1\1\1"
      Top
      
      Some extra notes:

      UltraEdit has the command Duplicate Line in menu Edit (using UltraEdit for Windows ≥ v23.20 with traditional menus or UltraEdit for Windows < v23.00) which duplicates the line the caret is currently positioned without selecting it. This command is also available as macro command DupeLine as it can be seen in Edit/Create Macro dialog and which is explained on help page opened on clicking in this dialog on button Help.

      UltraEdit does not support variables in macros as written many, many times in the forums. There is the macro command GetValue which prompts the macro user for an integer value. But on using this command in a macro the value entered by the user is always written into the file at current position of the caret. It is not possible to dynamically use the entered number in macro itself for example as loop number. It is necessary to use an UltraEdit script whenever variables are needed for a task.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         /* Verify the line termination of the last line of the file
            and insert one if the last line has no line termination. */
         UltraEdit.activeDocument.bottom();
         if (UltraEdit.activeDocument.isColNumGt(1))
         {
            UltraEdit.activeDocument.insertLine();
            if (UltraEdit.activeDocument.isColNumGt(1))
            {
               UltraEdit.activeDocument.deleteToStartOfLine();
            }
         }
         UltraEdit.activeDocument.top();
      
         // Prompt script user for the number of duplicates to produce.
         var nDuplicates = UltraEdit.getValue("Number of duplicates to produce:",1);
      
         if (nDuplicates > 0)
         {
            // Build the replace string depending on entered number.
            var sReplace = "";
            while (nDuplicates--)
            {
               sReplace += "\\1";
            }
            
            // Run the Perl regular expression Replace All for duplicating all lines.
            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;
            if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
            {
               UltraEdit.activeDocument.findReplace.searchInColumn=false;
            }
            UltraEdit.activeDocument.findReplace.preserveCase=false;
            UltraEdit.activeDocument.findReplace.replaceAll=true;
            UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
            UltraEdit.activeDocument.findReplace.replace("^(.*(:?\\r?\\n|\\r))\\K",sReplace);
      
            UltraEdit.activeDocument.top();
         }
      }
      
      Best regards from an UC/UE/UES for Windows user from Austria

      1032
      Power UserPower User
      1032

        Jan 28, 2018#3

        Mofi wrote:Run a Perl regular expression Replace All using a back-reference from top of file with search string ^(.*(:?\r?\n|\r))\K and replace string \1\1\1\1\1\1\1\1\1 which 9 times back-references the same found string kept unmodified in file.Copy that.
        It works very well as Replace All and also as macro command.

        Mofi wrote:UltraEdit has the command Duplicate Line in menu Edit (using UltraEdit for Windows ≥ v23.20 with traditional menus or UltraEdit for Windows < v23.00) which duplicates the line the caret is currently positioned without selecting it. This command is also available as macro command DupeLine as it can be seen in Edit/Create Macro dialog and which is explained on help page opened on clicking in this dialog on button Help
        Yes, I know that.
        But my question about to automatize duplicate lines is because I often handle a Virtualdub log created by plugin that put one line for each video frame.
        It's common to handle thousand of lines and I cannot use Alt+Shift+D (Duplicate line) for that.

        Mofi wrote:UltraEdit does not support variables in macros as written many, many times in the forums. There is the macro command GetValue which prompts the macro user for an integer value. But on using this command in a macro the value entered by the user is always written into the file at current position of the caret. It is not possible to dynamically use the entered number in macro itself for example as loop number. It is necessary to use an UltraEdit script whenever variables are needed for a task.
        I didn't know that.
        Sorry for the question about variables.

        But the best news: your script works very, very well!
        Many thanks!

        🙂🙂🙂