• Login
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.
  • S
    SENPAY98K
    last edited by Jan 6, 2022, 9:35 PM

    6071cf3c-96a0-4652-920e-c445f89c24e3-image.png
    i tried replacing it, but i have other similar lines in same file,
    so is there a way to delete/replace exactly the line number 15.

    P S 2 Replies Last reply Jan 6, 2022, 9:46 PM Reply Quote 0
    • P
      PeterJones @SENPAY98K
      last edited by PeterJones Jan 6, 2022, 10:03 PM Jan 6, 2022, 9:46 PM

      @senpay98k ,

      If you are saying, “I want to delete every line in all these files that contains start-of-line, 4 spaces, the digit two, and a comma, then end of line”, that’s easy: FIND = ^\x20{4}2,\R (there were 4 spaces between the ^ and the 2, but the forum collapsed them, so I edited the post to use the regex-equivalent \x20{4}), REPLACE = (make this box empty), SEARCH MODE = Regular Expression

      If you are saying, “I want to grab the contents of line 15 in my first file, then delete that line, and any line that matches that same text in any of the other files”: that cannot be done with find-in-files. The regex engine doesn’t have any “memory” across files, so it cannot know what line 15 looked like from the first file.

      If you are saying, “I want to delete just the 15th line in every file, no matter what the contents”, then that’s doable: FIND = (?-s)\A(^.*$\R?){14}\K(^.*$\R?), REPLACE = (make this box empty), SEARCH MODE = 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 3
      • S
        SENPAY98K @SENPAY98K
        last edited by Jan 7, 2022, 3:48 PM

        @senpay98k said in Delete the line number 15 in multiple files:

        6071cf3c-96a0-4652-920e-c445f89c24e3-image.png
        i tried replacing it, but i have other similar lines in same file,
        so is there a way to delete/replace exactly the line number 15.

        Does the work exactly as writen, thanks a lot :)

        1 Reply Last reply Reply Quote 1
        • R
          Robin Cruise
          last edited by Robin Cruise Jan 12, 2022, 8:55 AM Jan 12, 2022, 8:55 AM

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

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

          A 1 Reply Last reply Jan 12, 2022, 1:05 PM Reply Quote 0
          • 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.

                      ADEYINKA ADEWOYINA 1 Reply Last reply Aug 20, 2022, 8:04 AM Reply Quote 3
                      • ADEYINKA ADEWOYINA
                        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?

                          ADEYINKA ADEWOYINA 1 Reply Last reply Aug 20, 2022, 7:22 PM Reply Quote 1
                          • ADEYINKA ADEWOYINA
                            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