Community
    • Login

    Deleting First line and last two lines from multiple text files.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    macros
    11 Posts 6 Posters 2.7k 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.
    • Terry RT
      Terry R
      last edited by Terry R

      @R-k said in Deleting First line and last two lines from multiple text files.:

      delete first line and last two lines

      A very interesting problem. It turns out to be fairly simple. It requires use of a regex (regular expression). So the find in Files window needs the search mode set to “regular expression”.

      So with the Find in Files option the entries are:
      Find What:(?-s).+\R(?s)(.+)(?-s)(.+\R){2}
      Replace With:\1
      Use “Replace All” once the selection of files set.

      So what is occurring is:
      (?-s) we do NOT allow the . to include an end of line marker
      .+\R we grab the first line including end of line
      (?s) we change the option so the . DOES include end of line marker
      (.+) we grab everything else (initially), this is saved in group 1
      (?-s) we do NOT allow the . to include an end of line marker
      (.+\R){2} requires that we have the last 2 lines. This requires the previously saved group to give those 2 lines up.
      The replace field tells the system to return the saved group, which will now be minus the last 2 lines. The first line was already grabbed prior to group 1 being saved.

      Note, if the last line is an empty line (zero length line) this will still be counted as 1 of the 2 lines.

      Hopefully this will solve your problem. As always I suggest you have backup copies of the files being edited, and to randomly check some that were worked on just to verify the regex did what you needed.

      Terry

      1 Reply Last reply Reply Quote 4
      • guy038G
        guy038
        last edited by

        Hello, @r-k, @Terry-R and All,

        I found out an other solution, which is the opposite of the @Terry-r’s one : it deletes which is not needed, instead of rewriting which must be kept !

        However, because of some strange behaviours, due to the \A assertion, my solution requires two consecutive regex S/R :

        • The first S/R, below, will replace any possible range of empty line(s), at the very beginning of files, followed with the contents of the first non-empty line with a specific string, not yet present in files. I chose the string X_X_X_X_X_X, but any expression can be used as long as that expression does not exist in all the files scanned !

          • SEARCH (?-s)\A^\R*.+

          • REPLACE X_X_X_X_X_X

        • The second and last S/R, below, will delete the entire line X_X_X_X_X_X, with its line-break, as well as the last two non-empty lines, possibly followed with some true empty lines, at the very end of files

          • SEARCH X_X_X_X_X_X\R|(?-s).+\R.+\R*\z

          • REPLACE Leave EMPTY

        Best Regards,

        guy038

        1 Reply Last reply Reply Quote 3
        • Kosmos HuynhK
          Kosmos Huynh
          last edited by

          Dear all,

          I have a similar problem but I only want to delete the last two lines of multiple *txt files.
          I try your instructions but it does not work.

          My file 5fc0da5f-069e-44ec-961d-84d77d295a5f-image.png

          Many thanks for your help in advance!
          Huynh

          1 Reply Last reply Reply Quote 0
          • Terry RT
            Terry R
            last edited by

            @Kosmos-Huynh said in Deleting First line and last two lines from multiple text files.:

            but I only want to delete the last two lines of

            I was trying to alter my previous regex and decided I can do better (at least my test suggests it works).

            So using the Replace function (in your case you will likely use the “Find in Files”) we have:
            Find What:(?-s)\R(^.+)?\R(^.+)?\z
            Replace With: empty field here <— nothing in this field

            It doesn’t matters where in the file the cursor currently is as long as it is not within the last 2 lines. The “Wrap Around” option should be NOT ticked as once end of file reached it should have worked, no need to go back to start again. If using on the “Find in Files” function “Wrap Around” ticked or not probably doesn’t matter.

            So it should cater for empty lines.

            See if it solves your problem.

            Terry

            1 Reply Last reply Reply Quote 3
            • Terry RT
              Terry R
              last edited by

              @Terry-R said in Deleting First line and last two lines from multiple text files.:

              decided I can do better (at least my test suggests it works).

              Actually even better, I’ve proofed it a bit more and “pimped” it up.
              So we have
              Find What:(?-s)(\R(.+)?){2}\z

              Terry

              1 Reply Last reply Reply Quote 3
              • Kosmos HuynhK
                Kosmos Huynh
                last edited by

                Dear @Terry-R

                I worked as tested in a weird way. As you look at my previous figures. They treated line 178 as 6 lines. As a result, I had to change a little bit like the following:
                (?-s)(\R(.+)?){7}\z

                Many thanks

                1 Reply Last reply Reply Quote 0
                • Terry RT
                  Terry R
                  last edited by Terry R

                  @Kosmos-Huynh said in Deleting First line and last two lines from multiple text files.:

                  They treated line 178 as 6 lines

                  My regex treated the lines exactly correctly. Your image, now that I understand it slightly better shows 1 line (178) continuing over several “pseudo” lines due to enabling “Word Wrap”, which is under the “View” menu.

                  If you widen the Notepad++ window (or make it more narrow) then the apparent number of lines (as you call them) will be different. It you untick Word Wrap then you will get a different view with lines marching off (off screen) to the right.

                  So you need to understand the difference between what you call a line and what the “official” stance on line is. Officially a line has an “end of line” marker, thus the \R in my regex denotes this. My regex selects “2” lines using the “\R” as the marker to determine that, thus it does exactly what was asked. To view the “end of line” markers, use the option View, Show Characters, Show All Symbols. The “end of line” marker will be viewed as 2 squares in inverse highlighting (background coloured) with the characters “CR” and “LF” inside each one. This means Carriage Return and Line Feed.

                  Terry

                  1 Reply Last reply Reply Quote 2
                  • Terry RT
                    Terry R
                    last edited by

                    @Terry-R said in Deleting First line and last two lines from multiple text files.:

                    Officially a line has an “end of line” marker,

                    The exception is where the last (or only) line in a file has no “end of line” marker. It will be followed though by an “end of file” marker, which in my regex is the \z.

                    Terry

                    1 Reply Last reply Reply Quote 3
                    • Italo MarinhoI
                      Italo Marinho
                      last edited by

                      @Terry-R Seeing this made me want to study more of Regex coding, but I couldn’t figure out how would I solve a similar problem I’m having.

                      It’s very similar, actually. I want to erase the first 50 lines from my files, instead of last ones. How would I code that? Thanks for the attention!

                      Alan KilbornA 1 Reply Last reply Reply Quote 0
                      • Alan KilbornA
                        Alan Kilborn @Italo Marinho
                        last edited by

                        @Italo-Marinho said in Deleting First line and last two lines from multiple text files.:

                        I want to erase the first 50 lines from my files

                        Try searching for \A(?-s)(?:.*\R){50} and replacing with nothing.

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