Community
    • Login

    Duplicate Each Block of Lines Consecutively

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 2.8k Views 2 Watching
    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 Offline
      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 Offline
        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 Offline
          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

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors