Community
    • Login

    Delete the line number 15 in multiple files

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    14 Posts 5 Posters 3.1k 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.
    • Robin CruiseR
      Robin Cruise
      last edited by Robin Cruise

      “I want to delete just the 15th line in every file, no matter what the contents”

      FIND: \A(?:^.*$\R){14}\K.*$

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @Robin Cruise
        last edited by

        @robin-cruise said in Delete the line number 15 in multiple files:

        “I want to delete just the 15th line in every file, no matter what the contents”
        FIND: \A(?:^.$\R){14}\K.$

        That’s extremely similar to the @PeterJones solution.
        Do you have anything to add to explain why your solution meets the need any better, or why its difference is significant?

        Robin CruiseR 1 Reply Last reply Reply Quote 1
        • Robin CruiseR
          Robin Cruise @Alan Kilborn
          last edited by

          @alan-kilborn

          Do you have anything to add to explain why your solution meets the need any better, or why its difference is significant?

          Of course is a similar solution, except my regex is shorter. It is an alternative solution.

          Alan KilbornA 1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @Robin Cruise
            last edited by

            @robin-cruise said in Delete the line number 15 in multiple files:

            except my regex is shorter

            Shorter regexes aren’t always better.
            In fact, the regex engine has to work harder to find a match with your version.
            So I’d suggest not answering a solved issue a week later with just something shorter.

            Robin CruiseR 1 Reply Last reply Reply Quote 0
            • Robin CruiseR
              Robin Cruise @Alan Kilborn
              last edited by

              @alan-kilborn

              So I’d suggest not answering a solved issue a week later with just something shorter.

              as if to tell a car manufacturer not to produce small cars anymore, because it already has big cars …

              I believe that any alternative should be encouraged

              Alan KilbornA 1 Reply Last reply Reply Quote 0
              • Alan KilbornA
                Alan Kilborn @Robin Cruise
                last edited by

                @robin-cruise said in Delete the line number 15 in multiple files:

                believe that any alternative should be encouraged

                Right, especially if it slows down processing. OMG.

                PeterJonesP 1 Reply Last reply Reply Quote 0
                • PeterJonesP
                  PeterJones @Alan Kilborn
                  last edited by

                  For future readers: the solution proposed by @robin-cruise does not do the same thing as my solution. So depending on your definition of “delete the line number 15”, that “alternate” solution will fail.

                  Given the data

                  one
                  two
                  three
                  four
                  five
                  six
                  seven
                  eight
                  nine
                  ten
                  eleven
                  twelve
                  thirteen
                  fourteen
                  fifteen
                  sixteen
                  

                  the “alternate” solution \A(?:^.*$\R){14}\K.*$ will do one of two things: if you have . matches newline enabled, it will match the zero-width match at the very end of the file, deleting nothing, which is obviously not what is desired, by any interpretation of “delete line number 15”. If you have . matches newline unchecked, it will delete the contents of line 15, but still leave the newline:

                  one
                  two
                  three
                  four
                  five
                  six
                  seven
                  eight
                  nine
                  ten
                  eleven
                  twelve
                  thirteen
                  fourteen
                  
                  sixteen
                  

                  … with a gap (blank line) between fourteen and sixteen. This may not be what is desired.

                  On the other hand, my expression (?-s)\A(^.*$\R?){14}\K(^.*$\R?) will delete the whole line, including the newline sequence, regardless of the , matches newline setting.

                  one
                  two
                  three
                  four
                  five
                  six
                  seven
                  eight
                  nine
                  ten
                  eleven
                  twelve
                  thirteen
                  fourteen
                  sixteen
                  

                  This leaves no blank line between fourteen and sixteen.

                  Since the OP has already said that my solution did exactly what was desired for this question, having someone else providing an answer which results in anything different from the desired results is not presenting an alternative solution, it is presenting an incorrect “solution”, at least for this question. It may be useful to someone who does want to leave the blank line in line 15. but it’s not what the OP wanted.

                  ADEYINKA ADEWOYINA 1 Reply Last reply Reply Quote 3
                  • ADEYINKA ADEWOYINA
                    ADEYINKA ADEWOYIN @PeterJones
                    last edited by

                    @PeterJones Thanks for the solution, however, I used (?-s)\A(^.$\R?){14}\K(^.$\R?) to delete line number 1 in multiple files by changing ‘14’ in it to ‘0’ but seems not to work. Can you please help out?

                    Alan KilbornA 1 Reply Last reply Reply Quote 0
                    • Alan KilbornA
                      Alan Kilborn @ADEYINKA ADEWOYIN
                      last edited by Alan Kilborn

                      @ADEYINKA-ADEWOYIN said in Delete the line number 15 in multiple files:

                      I used (?-s)\A(^.$\R?){14}\K(^.$\R?) to delete line number 1 in multiple files by changing ‘14’ in it to ‘0’ but seems not to work.

                      I think you mean you tried:

                      (?-s)\A(^.*$\R?){0}\K(^.*$\R?)

                      [BTW, it is better to say exactly what you tried, instead of “I used this but I changed…”. It just helps maximize understanding.]

                      And while that is a bit of overkill for hitting the first line of a file, in my experiments I found it worked just fine.

                      Maybe discuss more exactly what you’re doing to achieve the failed result?

                      ADEYINKA ADEWOYINA 1 Reply Last reply Reply Quote 1
                      • ADEYINKA ADEWOYINA
                        ADEYINKA ADEWOYIN @Alan Kilborn
                        last edited by

                        @Alan-Kilborn How can I delete line number 1 in multiple files?

                        PeterJonesP 1 Reply Last reply Reply Quote 0
                        • PeterJonesP
                          PeterJones @ADEYINKA ADEWOYIN
                          last edited by

                          @ADEYINKA-ADEWOYIN ,

                          You do realize that @Alan-Kilborn already answered that question for you: the regex he showed worked for him. If it doesn’t work for you, you will have to provide more details , because that regex will work to delete the first line of the files. He even asked you to provide more details to explain how the given regex doesn’t work for you.

                          You really need to read this FAQ and see the referenes below.

                          That said, can be simplified: a large part of the original expression was only needed because the original post wanted to delete line#15, not line#1. To delete the first line is simpler: FIND = (?-s)\A(^.*$\R?), REPLACE is empty, and SEARCH MODE must be REGULAR EXPRESSION.

                          ----

                          Useful References

                          • Please Read Before Posting
                          • Template for Search/Replace Questions
                          • FAQ: Where to find regular expressions (regex) documentation
                          • Notepad++ Online User Manual: Searching/Regex
                          1 Reply Last reply Reply Quote 1
                          • First post
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors