Community
    • 登入

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

    已排程 已置頂 已鎖定 已移動 Help wanted · · · – – – · · ·
    macros
    11 貼文 6 Posters 3.1k 瀏覽
    正在載入更多貼文
    • 從舊到新
    • 從新到舊
    • 最多點贊
    回覆
    • 在新貼文中回覆
    登入後回覆
    此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
    • R kR
      R k
      最後由 編輯

      How i can delete first line and last two lines from multiple text file at once?

      1 條回覆 最後回覆 回覆 引用 0
      • Terry RT
        Terry R
        最後由 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 條回覆 最後回覆 回覆 引用 4
        • guy038G
          guy038
          最後由 編輯

          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 條回覆 最後回覆 回覆 引用 3
          • Kosmos HuynhK
            Kosmos Huynh
            最後由 編輯

            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 條回覆 最後回覆 回覆 引用 0
            • Terry RT
              Terry R
              最後由 編輯

              @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 條回覆 最後回覆 回覆 引用 3
              • Terry RT
                Terry R
                最後由 編輯

                @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 條回覆 最後回覆 回覆 引用 3
                • Kosmos HuynhK
                  Kosmos Huynh
                  最後由 編輯

                  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 條回覆 最後回覆 回覆 引用 0
                  • Terry RT
                    Terry R
                    最後由 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 條回覆 最後回覆 回覆 引用 2
                    • Terry RT
                      Terry R
                      最後由 編輯

                      @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 條回覆 最後回覆 回覆 引用 3
                      • Italo MarinhoI
                        Italo Marinho
                        最後由 編輯

                        @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 條回覆 最後回覆 回覆 引用 0
                        • Alan KilbornA
                          Alan Kilborn @Italo Marinho
                          最後由 編輯

                          @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 條回覆 最後回覆 回覆 引用 2
                          • 第一個貼文
                            最後的貼文
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors