• Login
Community
  • Login

Filling empty lines with previous line

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
7 Posts 6 Posters 1.3k 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.
  • V
    Viktoria Ontapado
    last edited by Oct 7, 2020, 3:38 PM

    Hi All!
    I’d like to request a bit of help with the following.

    I have a document with words and expressions and random number of empty lines between them.

    I’d like to fill all the empty ones with the preceding content. If the number of empty lines is more than one, every single one should repeat the previous “occupied” line.


    So from this one:

    apple
    watermelon
    empty line
    orange
    pear
    empty line
    empty line
    empty line
    empty line
    cherry and mango
    empty line
    strawberry
    empty line
    empty line
    nectarine
    plum, banana and raspberry
    empty line
    empty line
    empty line
    empty line
    empty line
    empty line
    grape
    empty line
    mango
    empty line
    blueberry and mandarin
    kiwi
    empty line
    empty line
    papaya


    I’d like to achieve the following one:

    apple
    watermelon
    watermelon
    orange
    pear
    pear
    pear
    pear
    pear
    cherry and mango
    cherry and mango
    strawberry
    strawberry
    strawberry
    nectarine
    plum, banana and raspberry
    plum, banana and raspberry
    plum, banana and raspberry
    plum, banana and raspberry
    plum, banana and raspberry
    plum, banana and raspberry
    plum, banana and raspberry
    grape
    grape
    mango
    mango
    blueberry and mandarin
    kiwi
    kiwi
    kiwi
    papaya


    What way can I achieve this?

    Thank you and have a nice day,
    Viktoria

    1 Reply Last reply Reply Quote 0
    • A
      Alan Kilborn
      last edited by Oct 7, 2020, 5:06 PM

      Do Replace All repeatedly with this regular-expression replacement:

      find: (?-s)^(.+?)\R\R
      repl: ${1}\r\n${1}\r\n

      Tick Wrap around

      1 Reply Last reply Reply Quote 3
      • G
        guy038
        last edited by guy038 Oct 7, 2020, 7:59 PM Oct 7, 2020, 7:58 PM

        Hello, @viktoria-ontapado, @alan-kilborn and All,

        This regex S/R should be OK, too :

        • SEARCH (?-s)(.+\R)\R

        • REPLACE \1\1

        • Tick the Wrap around option

        • Select the Regular expression search mode

        • Click, repeatedly, on the Replace All or, best, use the Alt + A shortcut

        Best Regards,

        guy038

        1 Reply Last reply Reply Quote 2
        • T
          Terry R
          last edited by Oct 8, 2020, 12:00 AM

          Hello, @viktoria-ontapado, @alan-kilborn, @guy038

          As an exercise to see if another way could be found I came up with this. I haven’t been able to create a 1 step regex, but these 2 different regexes each run only once.

          So the method is to move the lines around so the line to be copied is below those empty lines for each “set”. Then using a positive lookahead we capture the “line” to be copied and replace the current “empty” line with that text.

          Maybe it might prompt those amongst us (looking at you @guy038) to pull apart my idea to see if it can be made into a 1 step operation.

          First regex which changes the order of the lines in each set.
          Find What:(^.+\R)(\R+)
          Replace With:\2\1

          Second regex which fills in the empty lines with the text to be copied.
          Find What:(^\R)(?=\R*?(.+\R))
          Replace With:\2

          So each regex only needs running once using the “Replace All” button.

          Oh and contrary to above statement to have wrap around ticked IMO it should NOT be ticked. The cursor should be in the very first position of the file before beginning any of these regex, since the filling of empty lines is crucial on position of cursor when commencing.

          Terry

          1 Reply Last reply Reply Quote 3
          • M
            Makwana Prahlad Banned
            last edited by Oct 8, 2020, 3:31 AM

            Hello,@Viktoria-Ontapado

            Please follow this step, To Filling empty lines with the previous line

            Step 1:- Press Ctrl+H (Replace)
            Step 2:- Select Extended from SearchMode
            Step 3:- Put \r\n\r\n in Find What
            Step 4:- Put \r\n in ReplaceWith
            Step 5:- Click on Replace All

            I hope this information will be useful for you.
            Thank you.

            1 Reply Last reply Reply Quote -1
            • N
              Nick Brown
              last edited by Oct 8, 2020, 8:26 AM

              Alternatively using the python script plugin and the following python should achieve the desired results:

              from Npp import *
              
              def replaceEmptyLinesWithPrevious(contents, lineNumber, totalLines):
                  if len(contents.strip()) == 0:
                      previousLine = editor.getLine(lineNumber-1)
                      editor.replaceWholeLine(lineNumber, previousLine)
                  return 1
                  
              
              editor.forEachLine(replaceEmptyLinesWithPrevious)
              
              1 Reply Last reply Reply Quote 3
              • V
                Viktoria Ontapado
                last edited by Oct 8, 2020, 9:56 AM

                As usual, I really appreciate the answers, very impressive solutions. I tested the script as well, works like a charm. Thank you so much for the invaluable help.

                Have a nice day,
                Viktória

                1 Reply Last reply Reply Quote 2
                5 out of 7
                • First post
                  5/7
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors