• Login
Community
  • Login

Editing (deleting) non-contiguous lines

Scheduled Pinned Locked Moved General Discussion
8 Posts 5 Posters 542 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
    Moe Star
    last edited by Jan 12, 2024, 3:23 PM

    Hello All,
    New here but have been using Notepad++ for a few years.
    I frequently run into this problem and I am sure there is a way to do it but after searching the forums came up empty (might not have been using the correct search criterion though).
    I have a line with text I want to delete. Below it is another line I want to keep. This pattern continues for forty or fifty lines typically.
    Is there a way to just select the unwanted lines, delete them and also remove the carriage returns?

    Ex. of original
    Delete this line
    Keep this line
    Delete this line
    Keep this line
    (repeat)

    Desired outcome of edit
    Keep this line
    Keep this line
    (repeat)

    Thanks for reading!!
    Moe

    M 1 Reply Last reply Jan 12, 2024, 3:24 PM Reply Quote 0
    • M
      Moe Star @Moe Star
      last edited by Jan 12, 2024, 3:24 PM

      @Moe-Star
      er “incorrect ctiteion”

      P 1 Reply Last reply Jan 12, 2024, 3:34 PM Reply Quote 0
      • P
        PeterJones @Moe Star
        last edited by Jan 12, 2024, 3:34 PM

        @Moe-Star ,

        You appear to want to delete the “odd numbered” lines and keep the “even numbered” lines.

        If so,

        • FIND = (?-s)^.*\R(.*$)
          REPLACE = $1
          ☑ WRAP AROUND
          SEARCH MODE = Regular Expression
          REPLACE ALL

        before

        odd delete me
        even keep me
        odd delete this one too
        even keep also
        odd
        even
        odd
        even
        

        after

        even keep me
        even keep also
        even
        even
        
        M 1 Reply Last reply Jan 12, 2024, 5:19 PM Reply Quote 1
        • M
          Moe Star
          last edited by Jan 12, 2024, 3:50 PM

          Wow! Thanks for the speedy reply.
          I am sorry to mislead you but I am not a programmer so I am not sure exactly where I should be putting these commands (nor if there are variables I need to substitute).
          I was trying to the the Find command Replace tab. Is that the correct place to use these commands?
          Thanks again and sorry for the noobish questions.
          Moe

          A M 2 Replies Last reply Jan 12, 2024, 4:08 PM Reply Quote 0
          • A
            Alan Kilborn @Moe Star
            last edited by Alan Kilborn Jan 12, 2024, 4:09 PM Jan 12, 2024, 4:08 PM

            @Moe-Star said in Editing (deleting) non-contiguous lines:

            I was trying to the the Find command Replace tab. Is that the correct place to use these commands?

            As that is the only place these controls Peter cited are found:

            • FIND
            • REPLACE
            • WRAP AROUND
            • SEARCH MODE
            • REPLACE ALL

            You’ve really already answered a question you didn’t need to ask.

            1 Reply Last reply Reply Quote 1
            • M
              Mark Olson @Moe Star
              last edited by Mark Olson Jan 12, 2024, 4:09 PM Jan 12, 2024, 4:09 PM

              @Moe-Star
              Yes, Search->Find... from the main menu, then go to the Replace tab, then but Peter’s FIND in the Find what... box, and Peter’s REPLACE in the Replace with: box.

              I generally just use Ctrl+F to open the find form, and Ctrl+H to open the find/replace form, but you may have different keybindings.

              1 Reply Last reply Reply Quote 1
              • M
                mkupper @PeterJones
                last edited by Jan 12, 2024, 5:19 PM

                I understood @Moe-Star’s question to be to find some keyword or anchor and to then start deleting every other line. The ending condition is the vague “This pattern continues for forty or fifty lines typically.”

                @PeterJones’

                FIND = (?-s)^.\R(.$)
                REPLACE = $1

                works if someone is willing to copy/paste the forty or fifty lines typically into a new tab, run the search/replace/all which removes all of the odd numbered lines, and to copy/paste the results back.

                I often I do exactly that as I don’t want to bother with figuring and testing out a regular expression that makes changes to a section of text while also not affecting the file above or below that section.

                @Moe-Star, another way I often deal with this is to first insert what I call tags and then my set up my search/replace to only work on those lines that are tagged. For example, I have a file with many lines of text. I first do a search for ^_ to make sure there are no lines that start with _. If ^_ already exists then I pick another tag, maybe _del_. The tag can be any pattern that you like. I like using _ plus letters/digits for tags as I can double click a tag and all of that tag gets selected and visible in Notepad++.

                I then start adding a leading _ tag on those lines I will want to do a bulk search/replace operation on.

                Here’s something that may help you. Set up a set of lines with _ on every other line and then then load that into the copy/paste buffer as a column selection. You can then pick a spot in your file and paste will insert a _ in front of every other line. If you do something by accident then Ctrl-Z undo quietly and quickly removes the _ you just added.

                _
                
                _
                
                _
                
                _
                
                

                I usually use the keyboard and so column selections are created by holding the Shift+Alt keys down while using up or down arrow plus one right or left arrow to select the _ characters. You can do the same with Alt (or Shift+Alt) and then dragging the mouse.

                Once I’m done with tagging the search replace becomes easy
                Search: ^_whatever I want
                Replace: whatever I want

                To delete or mark the tagged lines I search for ^_.*\R and replace with nothing.

                1 Reply Last reply Reply Quote 1
                • M
                  Moe Star
                  last edited by Jan 13, 2024, 12:51 PM

                  Wow again!
                  Great group and very helpful people!
                  I will give it a go again with these tips.
                  Thanks a bunch.

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