• Login
Community
  • Login

Delete the line number 15 in multiple files

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
14 Posts 5 Posters 3.9k 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.
  • A
    Alan Kilborn @Robin Cruise
    last edited by Jan 12, 2022, 1:05 PM

    @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?

    R 1 Reply Last reply Jan 13, 2022, 1:48 PM Reply Quote 1
    • R
      Robin Cruise @Alan Kilborn
      last edited by Jan 13, 2022, 1:48 PM

      @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.

      A 1 Reply Last reply Jan 13, 2022, 1:59 PM Reply Quote 0
      • A
        Alan Kilborn @Robin Cruise
        last edited by Jan 13, 2022, 1:59 PM

        @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.

        R 1 Reply Last reply Jan 13, 2022, 4:07 PM Reply Quote 0
        • R
          Robin Cruise @Alan Kilborn
          last edited by Jan 13, 2022, 4:07 PM

          @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

          A 1 Reply Last reply Jan 13, 2022, 4:19 PM Reply Quote 0
          • A
            Alan Kilborn @Robin Cruise
            last edited by Jan 13, 2022, 4:19 PM

            @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.

            P 1 Reply Last reply Jan 13, 2022, 5:59 PM Reply Quote 0
            • P
              PeterJones @Alan Kilborn
              last edited by Jan 13, 2022, 5:59 PM

              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.

              A 1 Reply Last reply Aug 20, 2022, 8:04 AM Reply Quote 3
              • A
                ADEYINKA ADEWOYIN @PeterJones
                last edited by Aug 20, 2022, 8:04 AM

                @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?

                A 1 Reply Last reply Aug 20, 2022, 11:09 AM Reply Quote 0
                • A
                  Alan Kilborn @ADEYINKA ADEWOYIN
                  last edited by Alan Kilborn Aug 20, 2022, 11:11 AM Aug 20, 2022, 11:09 AM

                  @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?

                  A 1 Reply Last reply Aug 20, 2022, 7:22 PM Reply Quote 1
                  • A
                    ADEYINKA ADEWOYIN @Alan Kilborn
                    last edited by Aug 20, 2022, 7:22 PM

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

                    P 1 Reply Last reply Aug 20, 2022, 9:39 PM Reply Quote 0
                    • P
                      PeterJones @ADEYINKA ADEWOYIN
                      last edited by Aug 20, 2022, 9:39 PM

                      @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