Community
    • Login

    Duplicate Each Block of Lines Consecutively

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 1.4k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      Mayonnaisu
      last edited by Mayonnaisu

      Hello everyone, I’ve been trying to find a way to duplicate each block of lines consecutively. The only thing I found so far is to use CTRL+D, but it will be troublesome to use it on a large text file since I have to do it manually for each block. Is there any way to do it at once using Notepad++?

      This is the example:

      1
      line 1
      line 1a
      
      2
      line 2
      line 2a
      
      3
      line 3
      line 3a
      

      This is the desired result:

      1
      line 1
      line 1a
      
      1
      line 1
      line 1a
      
      2
      line 2
      line 2a
      
      2
      line 2
      line 2a
      
      3
      line 3
      line 3a
      
      3
      line 3
      line 3a
      
      PeterJonesP 1 Reply Last reply Reply Quote 2
      • PeterJonesP
        PeterJones @Mayonnaisu
        last edited by

        @Mayonnaisu said in Duplicate Each Block of Lines Consecutively:

        Thanks for using code blocks. it made it easy to copy/paste and test the regex.

        The simplest would be to use FIND = (?s)^.*?\R\R , replace = $0$0 with regular-expression mode – that allows . to match newlines, and finds the shortest sequence that has two newlines in a row (the \R\R).
        But unless you had two blank lines after the 3rd group, that wouldn’t work for the third group.

        Some experimenting showed that the most reliable was FIND = (?s)^(.*?)(\R)(\R|\Z) and REPLACE = $1$2$2$1$2$2 – that way, there doesn’t have to be two blank lines (though there does need to be a newline after the third group).

        ----

        Useful References

        • Please Read Before Posting
        • Template for Search/Replace Questions
        • Formatting Forum Posts
        • Notepad++ Online User Manual: Searching/Regex
        • FAQ: Where to find other regular expressions (regex) documentation
        M 1 Reply Last reply Reply Quote 3
        • M
          Mayonnaisu @PeterJones
          last edited by

          @PeterJones Thanks for the solution! I really appreciate that. So it turns out that I can use regex to do it. I’m embarassed that my regex skill hasn’t improved much. It seems to decline instead after long hiatus. If time allows, I want to relearn it so that I don’t have to keep asking here.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors