• Login
Community
  • Login

How to remove all lines before a string value including the line with a string

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
5 Posts 3 Posters 1.5k 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.
  • M
    Mark Harney
    last edited by Feb 10, 2020, 8:46 PM

    Hi,

    I am looking for a way to have Notepad ++ remove all the lines at the beginning of text files that come before a string, as well as that line. Example:

    In the list below, representing the contents of a text file, I would like to remove all lines before the word blah, as well as that line. How can I do it? I need to do it to a folder full of such files.

    There is a blank line after that text as well that I would like to remove, as in the example. I would like the result to start with “more stuff” on the first line of the resulting text file. There are other blank lines above blah, but blah is always present to trigger on.

    test1
    test2
    testing
    Hi there
    blah

    more stuff
    more stuff even
    etc
    etc
    My dog has fleas

    1 Reply Last reply Reply Quote 0
    • S
      Sofistanpp
      last edited by Feb 10, 2020, 9:47 PM

      -Place the cursor at the begining of the file and try the following regex (only tested in the example you provided):

      Search: (?s).*(?-s)^blah.*\R{2}
      Replace: [Leave it in blank]
      

      Once you are sure that it removes the unwanted lines, apply it to the other files.

      Hope this helps.

      1 Reply Last reply Reply Quote 2
      • M
        Mark Harney
        last edited by Feb 11, 2020, 4:39 PM

        @Sofistanpp said in How to remove all lines before a string value including the line with a string:

        (?s).(?-s)^blah.\R{2}

        Thanks for that! It didn’t work for me, but here is the actual stuff I am trying to work with. What I need to do is key on a line that reads:

        Content-Type: message/rfc822

        The line will always be the same. RFC822 appears before this in the error message, so the phrase has to be a little more specific than just using that. Triggering on that whole line is fine, or some part of it that is unique would be ok. I think just using:

        message/rfc822

        would be ok.

        So I need to remove that line and everything above it, and the blank line below it.

        I would post more of the content but the site is marking it as spam. Bad BAD me… :/

        1 Reply Last reply Reply Quote 0
        • P
          PeterJones
          last edited by Feb 11, 2020, 5:07 PM

          (?s).*(?-s)^blah.*\R{2}
          It didn’t work for me,

          Of course blah won’t work if you’re searching for Content-Type: message/rfc822. Did you try for Content-Type: message/rfc822? Because (?s).*(?-s)^Content-Type: message/rfc822.*\R{2} works for me:

          4ba0c0bf-2fdd-448e-88c4-a99f450e6551-image.png

          If you’re trying a substring, like just message/rfc822, then you cannot use the ^ to anchor to the start of the line; or, if you want to keep the anchor, you need to consume the text between, like with (?s).*(?-s)^.*message/rfc822.*\R{2}

          1 Reply Last reply Reply Quote 2
          • M
            Mark Harney
            last edited by Feb 11, 2020, 6:25 PM

            @PeterJones said in How to remove all lines before a string value including the line with a string:

            (?s).(?-s)^Content-Type: message/rfc822.\R{2}

            I had just tried with rfc822 and it did not work, but I have tried again with the string you gave me and it works. I really appreciate your help!

            Mark

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