Community
    • Login

    How do you get a macro recording to delete a certain number of lines?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    7 Posts 4 Posters 683 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.
    • Tibor SütschT
      Tibor Sütsch
      last edited by Tibor Sütsch

      Before I import data from a csv file into Excel, I first edit it with Notepad++. So far it’s working very well! Now I wanted to record a new macro that always deletes a certain number of lines in the middle of the text in every csv file. Unfortunately, the newly recorded macro works the same as the previous one. The new task that is supposed to delete a certain number of lines is ignored. Does anyone have a tip on how I can get this into the macro recording? If that’s not possible, I would ask you to program this function in the next possible update. Thanks in advance!
      I’m using for this the newest version of Notepad++ v8.6 (64-bit) installed on usb stick as portable mode.

      mkupperM 1 Reply Last reply Reply Quote 1
      • mkupperM
        mkupper @Tibor Sütsch
        last edited by mkupper

        @Tibor-Sütsch If you are using Notepad++'s find or search function to get to the spot in the middle of the CSV file then you can do a search/replace with the replacement part being nothing (empty) to delete those lines.

        For example, let’s say you want to delete a line that contains the phrase “xyzzy” and the following two lines meaning you will be deleting three lines.

        Search: .*xyzzy.*\R.*\R.*\R
        Replace: (nothing or empty)

        The macro can do Ctrl-H .*xyzzy.*\R.*\R.*\R tab delete Alt+A Esc

        If you regularly switch your search/find mode between regular expressions or and normal/extended then you should include Alt-G to activate regular expression mode.

        Tibor SütschT 1 Reply Last reply Reply Quote 1
        • Tibor SütschT
          Tibor Sütsch @mkupper
          last edited by

          @mkupper
          hello mkupper,
          thank you for the fast reaction.
          I already use search and replace in the macro I recorded so far. Which also works very well.
          In addition, now I wanted to automatically delete everything from the 2nd to the 235th line. This leaves me with the remaining 67 lines of 301. But the problem is that there are no identical or identifiable groups of characters in these lines that need to be deleted. So it remains me only the orientation 2nd to the 235th lines. And as far as I understand it, unfortunately this no longer works with search and replace.

          Terry RT Alan KilbornA 2 Replies Last reply Reply Quote 0
          • Terry RT
            Terry R @Tibor Sütsch
            last edited by

            @Tibor-Sütsch
            Just thinking slightly out of the box here.

            I’m not on a PC currently to test, but if all the lines were numbered 1 onwards a regular expression would then bookmark the relevant numbered lines. Those would then be deleted and then the remaining lines would have those lines numbers removed.

            I think that is all achievable using a macro.

            I’ll test a bit later and hopefully come back with a solution.

            Terry

            1 Reply Last reply Reply Quote 0
            • Alan KilbornA
              Alan Kilborn @Tibor Sütsch
              last edited by Alan Kilborn

              @Tibor-Sütsch

              You can delete lines M through N (inclusive) in a file by doing this:

              • calculate X = M - 1
              • calculate Y = N - X
              • ReplaceAll \A(?:.*\R){X}\K(?:.*\R){Y} with nothing (checkmarking Regular expression and Wrap around) – after plugging in the calculated numbers for X and Y)
              Tibor SütschT 1 Reply Last reply Reply Quote 4
              • Tibor SütschT
                Tibor Sütsch @Alan Kilborn
                last edited by

                @Alan-Kilborn
                Hello Alan,

                I don’t understand the individual commands (not yet). ;) But I entered the values for my lines and it works exactly as I imagine! Perfect!
                Thank you very much!
                Wish you all a Merry Christmas!

                mkupperM 1 Reply Last reply Reply Quote 2
                • mkupperM
                  mkupper @Tibor Sütsch
                  last edited by

                  @Tibor-Sütsch, another way to do it that is similar to the first version I posted is:
                  Search: (.*xyzzy.*\R)(?:.*\R)*?(.*plugh.*)
                  Replace: nothing or empty

                  That will delete all lines starting with one that contains the string xyzzy and ending with one that contains plugh. I put the (.*xyzzy.*\R) and (.*plugh.*) parts in parentheses so that you have the option of using either \1 and or \2 in the replacement part if you need to keep either of those. For example, to delete everything after xyzzy and up to and including plugh the replacement part would be \1.

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